我有一个带有剪切路径的大图像(至少 200 MB 和最大 2 GB)。我想应用剪切路径来移除背景。到目前为止,我发现的唯一解决方案 ( ConvertClippingPathToMask ) 使用位图,它将整个图像加载到内存中并引发 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");
}
}
通过这种方法,始终需要位图来获取图形对象,然后再应用剪切路径。
实际上,我什至不需要maskBitmap
in Memory,因为我可以为 setAlpha 使用单独的阅读器,但是:如何在没有位图的情况下创建 maskBitmap 来创建图形对象?