Create Clipping Path

Write Drawing Clipping Paths

Demonstrates how to create Adobe resource block with path from SVG file and save it to JPEG format.

Сode Snippet

// Internally a shape may be presented in many different ways, so we need to do this processing.
public static void CombineShapes(GraphicsContainer container, Path accumulatePath)
{
    foreach (var shapeItem in container.Items.OfType<ShapeItem>())
    {
        shapeItem.Path.ApplyTransform(shapeItem.Transform);
        accumulatePath.DrawPath(shapeItem.Path);
    }

    foreach (var containerItem in container.Items.OfType<ContainerItem>())
    {
        CombineShapes(containerItem.GraphicsContainer, accumulatePath);
    }
}

public static void Run()
{
    using (var reader = new SvgReader("cat.svg"))
    using (var gc = reader.GetContent())
    using (var bitmap = new Bitmap((int)gc.Width, (int)gc.Height, PixelFormat.Format24bppRgb, RgbColor.White))
    using (var gr = bitmap.GetAdvancedGraphics())
    using (var shape = new Path())
    {
        // Read a path from the SVG file
        CombineShapes(gc, shape);

        // https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/
        const int pathInfoKey = 0x07D0;

        // Create an adobe resource block with image and save to the dictionary.
        var ar = new AdobeResourceDictionary();
        var arb = AdobeResourceBlock.CreatePath(shape, bitmap.Width, bitmap.Height);

        ar.Add(pathInfoKey, new AdobeResourceBlock("Path name", arb));

        // Draw the path we got on the bitmap.
        gr.FillPath(new SolidBrush(RgbColor.Black), shape);

        // Save the output
        bitmap.Save("cat.jpg", new JpegSettings() { AdobeResources = ar });
    }
}

Input

cat.svg

Output

cat.jpg

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