1

我有一个带有剪切路径的大图像(至少 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");
        }
    }

通过这种方法,始终需要位图来获取图形对象,然后再应用剪切路径。

实际上,我什至不需要maskBitmapin Memory,因为我可以为 setAlpha 使用单独的阅读器,但是:如何在没有位图的情况下创建 maskBitmap 来创建图形对象?

4

2 回答 2

0

为了完整起见:来自Aurigma.Forums
的 Fedor 的这个解决方案涵盖了 Eugenes 解决方案没有的一些案例。

using (var reader = ImageReader.Create("../../../PathForTest.tif"))
using (var maskGen = new ImageGenerator(reader.Width, reader.Height, PixelFormat.Format8bppGrayscale, RgbColor.Black))
using (var drawer = new Drawer())
using (var bitmap = new Bitmap())
{
    var graphicsPath = Aurigma.GraphicsMill.AdvancedDrawing.Path.Create(reader.ClippingPaths[0], reader.Width, reader.Height);

    drawer.Draw += (sender, e) =>
    {
        e.Graphics.FillPath(new SolidBrush(new GrayscaleColor(255)), graphicsPath);
    };

    using (var setAlpha = new SetAlpha(maskGen + drawer))
    {
        (reader + setAlpha + bitmap).Run();
        bitmap.Save("../../../result.tif");
    }
}
于 2017-06-02T12:47:18.330 回答
0

正确的方法是使用 Pipeline API:

using (var reader = new JpegReader("ImageWithPath.jpg"))
using (var gc = new GraphicsContainer(reader))
using (var ig = new ImageGenerator(gc, PixelFormat.Format8bppGrayscale, RgbColor.Black))
using (var setAlpha = new Aurigma.GraphicsMill.Transforms.SetAlpha())
{
    using (var gr = gc.GetGraphics())
    {
        var path = reader.ClippingPaths[0].CreateGraphicsPath(reader.Width, reader.Height);
        gr.FillPath(new SolidBrush(RgbColor.White), Aurigma.GraphicsMill.AdvancedDrawing.Path.Create(path));
    }

    setAlpha.AlphaSource = ig;

    Pipeline.Run(reader + setAlpha + "output.png");
}
于 2017-05-29T04:04:02.190 回答