---
title: "Modify Clipping Path Explicitly (Memory Friendly)"
canonical: https://www.graphicsmill.com/samples/Metadata/ClippingPath/ModifyClippingPathExplicitlyMemoryFriendly_cs
product: "Graphics Mill"
source: "Metadata/ClippingPath/ModifyClippingPathExplicitlyMemoryFriendly.cs"
---

# Modify Clipping Path Explicitly (Memory Friendly)

Modifies clipping path explicitly using memory-friendly Pipeline API.

## Instruction

Modify an image by combining it with a generated white background, adjusting its clipping path coordinates, and saving it with the updated clipping path as a JPEG.

## Usings

```csharp
using Aurigma.GraphicsMill.Codecs;
using Aurigma.GraphicsMill.Transforms;
```

## Code Snippet

```csharp
int width = 1000;
int height = 1000;

using (var reader = new JpegReader("Apple.jpg"))
using (var generator = new ImageGenerator(width, height, reader.PixelFormat, RgbColor.White))
using (var combiner = new Combiner(CombineMode.Copy))
using (var writer = new JpegWriter("ModifyClippingPathExplicitlyMemoryFriendly.jpg"))
{
    combiner.TopImage = reader;
    combiner.X = (width - reader.Width) / 2;
    combiner.Y = (height - reader.Height) / 2;

    // The clipping path has relatives coordinates (0.0f ... 1.f0). So we need to transform it.
    var transform = new System.Drawing.Drawing2D.Matrix();
    transform.Scale((float)reader.Width / (float)width, (float)reader.Height / (float)height);
    transform.Translate((float)combiner.X / (float)reader.Width, (float)combiner.Y / (float)reader.Height);

    var adobeResources = reader.AdobeResources;

    // Remove clipping paths
    foreach (long key in adobeResources.Keys)
    {
        if (key >= FirstPathId && key <= LastPathId)
        {
            adobeResources.Remove(key);
        }
    }

    // Transform and save clipping paths
    for (var i = 0; i < reader.ClippingPaths.Count; i++)
    {
        var clippingPath = reader.ClippingPaths[i];
        clippingPath.ApplyTransform(transform);

        adobeResources.Add(FirstPathId + i, new AdobeResourceBlock(clippingPath.Name, clippingPath.Data));
    }

    writer.AdobeResources = adobeResources;

    Pipeline.Run(generator + combiner + writer);
}
```

## Explanation

The code:

- Reads the "Apple.jpg" image.
- Generates a new image with specified width and height, using a white background.
- Combines the original image onto the center of the generated image.
- Transforms the clipping path relative coordinates to match the new image dimensions.
- Removes any existing clipping paths within the Adobe resources and adds the transformed clipping paths.
- Saves the result as a JPEG image, including the updated clipping paths in the Adobe resources, to "ModifyClippingPathExplicitlyMemoryFriendly.jpg".

## Tags

- [Clipping Paths](<https://www.graphicsmill.com/samples/tags/clipping-paths_md>): Reading and writing Adobe resources clipping path
- [Crop](<https://www.graphicsmill.com/samples/tags/crop_md>): Cropping images
- [Pipeline](<https://www.graphicsmill.com/samples/tags/pipeline_md>): Working with images via the Pipeline API
- [Read](<https://www.graphicsmill.com/samples/tags/read_md>): Reading images with codecs
- [Write](<https://www.graphicsmill.com/samples/tags/write_md>): Writing images with codecs

## Input

- [Apple.jpg](<https://www.graphicsmill.com/samples/Metadata/compiled/Debug/Apple_jpg>) (preview)

## Output

- [ModifyClippingPathExplicitlyMemoryFriendly.jpg](<https://www.graphicsmill.com/samples/Metadata/compiled/Debug/ModifyClippingPathExplicitlyMemoryFriendly_jpg>) (preview)

## Used APIs

- [Aurigma.GraphicsMill.Codecs.AdobeResourceBlock](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_AdobeResourceBlock.htm.md>)
- [Aurigma.GraphicsMill.Codecs.ClippingPath.Path.ApplyTransform(Matrix)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_ClippingPath_Path_ApplyTransform_1_5bff9483.htm.md>)
- [Aurigma.GraphicsMill.Codecs.ClippingPath.Path.Data](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_ClippingPath_Path_Data.htm.md>)
- [Aurigma.GraphicsMill.Codecs.ClippingPath.Path.Name](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_ClippingPath_Path_Name.htm.md>)
- [Aurigma.GraphicsMill.Codecs.ImageReader.AdobeResources](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_ImageReader_AdobeResources.htm.md>)
- [Aurigma.GraphicsMill.Codecs.ImageReader.ClippingPaths](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_ImageReader_ClippingPaths.htm.md>)
- [Aurigma.GraphicsMill.Codecs.ImageReader.PixelFormat](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_ImageReader_PixelFormat.htm.md>)
- [Aurigma.GraphicsMill.Codecs.JpegReader](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_JpegReader.htm.md>)
- [Aurigma.GraphicsMill.Codecs.JpegWriter](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_JpegWriter.htm.md>)
- [Aurigma.GraphicsMill.Codecs.JpegWriter.AdobeResources](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_JpegWriter_AdobeResources.htm.md>)
- [Aurigma.GraphicsMill.Codecs.MetadataDictionary.Add(Object,Object)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_MetadataDictionary_Add_2_df764941.htm.md>)
- [Aurigma.GraphicsMill.Codecs.MetadataDictionary.Keys](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_MetadataDictionary_Keys.htm.md>)
- [Aurigma.GraphicsMill.Codecs.MetadataDictionary.Remove(Object)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_MetadataDictionary_Remove_1_d02429b3.htm.md>)
- [Aurigma.GraphicsMill.ImageGenerator](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_ImageGenerator.htm.md>)
- [Aurigma.GraphicsMill.Transforms.CombineMode](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Transforms_CombineMode.htm.md>)
- [Aurigma.GraphicsMill.Transforms.Combiner](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Transforms_Combiner.htm.md>)
- [Aurigma.GraphicsMill.Transforms.Combiner.TopImage](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Transforms_Combiner_TopImage.htm.md>)
- [Aurigma.GraphicsMill.Transforms.Combiner.X](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Transforms_Combiner_X.htm.md>)
- [Aurigma.GraphicsMill.Transforms.Combiner.Y](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Transforms_Combiner_Y.htm.md>)
