Create Clipping Path
View as MarkdownDemonstrates how to create Adobe resource block with path from SVG file and save it to JPEG format.
Code 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.GetGraphics())
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
Used APIs
- Aurigma.GraphicsMill.Bitmap.GetGraphics
- Aurigma.GraphicsMill.Codecs.AdobeResourceBlock
- Aurigma.GraphicsMill.Codecs.AdobeResourceBlock.CreatePath(Path,Int32,Int32)
- Aurigma.GraphicsMill.Codecs.AdobeResourceDictionary
- Aurigma.GraphicsMill.Codecs.JpegSettings
- Aurigma.GraphicsMill.Codecs.JpegSettings.AdobeResources
- Aurigma.GraphicsMill.Codecs.MetadataDictionary.Add(Object,Object)
- Aurigma.GraphicsMill.Codecs.SvgReader
- Aurigma.GraphicsMill.Codecs.SvgReader.GetContent
- Aurigma.GraphicsMill.Drawing.ContainerItem
- Aurigma.GraphicsMill.Drawing.ContainerItem.GraphicsContainer
- Aurigma.GraphicsMill.Drawing.Graphics.FillPath(Brush,Path)
- Aurigma.GraphicsMill.Drawing.GraphicsContainer
- Aurigma.GraphicsMill.Drawing.GraphicsContainer.Items
- Aurigma.GraphicsMill.Drawing.Item.Transform
- Aurigma.GraphicsMill.Drawing.Path
- Aurigma.GraphicsMill.Drawing.Path.ApplyTransform(Matrix)
- Aurigma.GraphicsMill.Drawing.Path.DrawPath(Path)
- Aurigma.GraphicsMill.Drawing.ShapeItem
- Aurigma.GraphicsMill.Drawing.ShapeItem.Path
- Aurigma.GraphicsMill.Drawing.SolidBrush