Convert Clipping Path To Mask

View as Markdown

ChannelsClipping PathsDrawingMetadata

Converts clipping path to alpha channel mask.

Code Snippet

using (var reader = new JpegReader("Apple.jpg"))
using (var bitmap = reader.Frames[0].GetBitmap())
using (var maskBitmap = new Bitmap(bitmap.Width, bitmap.Height, PixelFormat.Format8bppGrayscale, new GrayscaleColor(0)))
using (var graphics = maskBitmap.GetGraphics())
{
    var graphicsPath = reader.ClippingPaths[0].CreateGraphicsPath(reader.Width, reader.Height);

    graphics.FillPath(new SolidBrush(new GrayscaleColor(255)), Path.Create(graphicsPath));

    bitmap.Channels.SetAlpha(maskBitmap);

    bitmap.Save("ConvertClippingPathToMask.png");
}

Input

Apple.jpg

Apple.jpg

Output

ConvertClippingPathToMask.png

ConvertClippingPathToMask.png

Used APIs