Modify Clipping Path Explicitly (Memory Friendly)
View as MarkdownModifies clipping path explicitly using memory-friendly Pipeline API.
Code 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
Used APIs
- Aurigma.GraphicsMill.Codecs.AdobeResourceBlock
- Aurigma.GraphicsMill.Codecs.ClippingPath.Path.ApplyTransform(Matrix)
- Aurigma.GraphicsMill.Codecs.ClippingPath.Path.Data
- Aurigma.GraphicsMill.Codecs.ClippingPath.Path.Name
- Aurigma.GraphicsMill.Codecs.ImageReader.AdobeResources
- Aurigma.GraphicsMill.Codecs.ImageReader.ClippingPaths
- Aurigma.GraphicsMill.Codecs.ImageReader.PixelFormat
- Aurigma.GraphicsMill.Codecs.JpegReader
- Aurigma.GraphicsMill.Codecs.JpegWriter
- Aurigma.GraphicsMill.Codecs.JpegWriter.AdobeResources
- Aurigma.GraphicsMill.Codecs.MetadataDictionary.Add(Object,Object)
- Aurigma.GraphicsMill.Codecs.MetadataDictionary.Keys
- Aurigma.GraphicsMill.Codecs.MetadataDictionary.Remove(Object)
- Aurigma.GraphicsMill.ImageGenerator
- Aurigma.GraphicsMill.Transforms.CombineMode
- Aurigma.GraphicsMill.Transforms.Combiner
- Aurigma.GraphicsMill.Transforms.Combiner.TopImage
- Aurigma.GraphicsMill.Transforms.Combiner.X
- Aurigma.GraphicsMill.Transforms.Combiner.Y