I have a large image (at least 200 MB and up to 2 GB) with a clipping path. I want to apply the clipping path to remove the background. The only solution (ConvertClippingPathToMask) I have found so far uses a Bitmap, which loads the entire image into memory and throws an OutOfMemoryException.
/// <summary>
/// Converts clipping path to alpha channel mask
/// </summary>
private static void ConvertClippingPathToMask()
{
using (var reader = new JpegReader("../../../../_Input/Apple.jpg"))
using (var bitmap = reader.Frames[0].GetBitmap()) // I can get rid of this line by using reader instead of bitmap in the next line, but then the OOM will throw in the next line.
using (var maskBitmap = new Bitmap(bitmap.Width, bitmap.Height, PixelFormat.Format8bppGrayscale, new GrayscaleColor(0)))
using (var graphics = maskBitmap.GetAdvancedGraphics())
{
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("../../../../_Output/ConvertClippingPathToMask.png");
}
}
By this approach a bitmap is always necessary to get the graphics object, which then applies the clipping path.
Actually, I don't even need the maskBitmap
in Memory because I can use a separate reader for setAlpha, but then: How do I create the maskBitmap without a bitmap to create the graphics object from?