Modify Clipping Path Explicitly (Memory Friendly)

Clipping Paths Read Write Pipeline Crop

Modifies clipping path explicitly using memory-friendly Pipeline API.

Сode Snippet

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);
}

Input

Apple.jpg

Output

ModifyClippingPathExplicitlyMemoryFriendly.jpg

For AI-assisted development: Download Graphics Mill Code Samples XML Catalog