如何使用Graphicsmill中的管道使用clippingPath删除图像的背景?

时间:2017-05-09 13:01:02

标签: aurigma

我有一个带有剪切路径的大图像(至少200 MB,最多2 GB)。我想应用剪切路径来删除背景。到目前为止我找到的唯一解决方案(ConvertClippingPathToMask)使用了一个Bitmap,它将整个图像加载到内存中并抛出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,因为我可以为setAlpha使用单独的阅读器,但是:如何创建没有位图的maskBitmap来创建图形对象?

2 个答案:

答案 0 :(得分: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");
}

答案 1 :(得分:0)

为了完整起见:
Fedor从Aurigma.Forums的解决方案涵盖了一些案例,而Eugenes解决方案则没有。

for obj in geometryObject.values {
    print(obj)
    if let type = obj as? String {
        typeData = type
    } 
    if let coordObj = obj as? [Double] {
        coord = Coordinates(latitude: coordObj[0], longitude: coordObj[1])
        coordData?.append(coord)
    }
    //Add one more if let condition
    if let coordObj = obj as? [[Double]] { 
        for coordinate in coordObj {
            let coord = Coordinates(latitude: coordinate[0], longitude: coordinate[1])
            coordData?.append(coord)
        }
    }
    geometries = Geometry(type: typeData, coordinates: coordData!)
}
let feature = Feature(type: type, properties: properties!, geometry: geometries!)
features.append(feature)