简体   繁体   English

裁剪 svg 图像并将其转换为 png

[英]Crop a svg image and convert it to png

I have a big svg image.我有一个大的 svg 图像。
I would like to crop it to a rectangle, using coordinates, and convert it to png image.我想使用坐标将其裁剪为矩形,并将其转换为 png 图像。

I have to say that I'm not used to drawing with c#.不得不说我不习惯用c#作图。
Surface, Canvas and other notions are new to me.表面,Canvas 和其他概念对我来说是新的。

I have figured out how to load the svg, using SkiaShark and SkiaShark.Svg:我已经弄清楚如何使用 SkiaShark 和 SkiaShark.Svg 加载 svg:

var svg = new SkiaSharp.Extended.Svg.SKSvg();
svg.Load(tmpPath);

And found a gist that saves a png.并找到了一个保存 png 的要点 But this is "Chinese" for me.但这对我来说是“中国人”。

var imageInfo = new SKImageInfo(100, 100);
using (var surface = SKSurface.Create(imageInfo))
using (var canvas = surface.Canvas)
{
    // calculate the scaling need to fit to screen
    var scaleX = 100 / svg.Picture.CullRect.Width;
    var scaleY = 100 / svg.Picture.CullRect.Height;
    var matrix = SKMatrix.CreateScale((float)scaleX, (float)scaleY);

    // draw the svg
    canvas.Clear(SKColors.Transparent);
    canvas.DrawPicture(svg.Picture, ref matrix);
    canvas.Flush();

    using (var data = surface.Snapshot())
    using (var pngImage = data.Encode(SKEncodedImageFormat.Png, 100))
    {
        tmpPath = Path.ChangeExtension(tmpPath, "PNG");
        using var imageStream = new FileStream(tmpPath, FileMode.Create);
        pngImage.SaveTo(imageStream);
    }
}

If someone could show me the directions, it would be much appreciated.如果有人能给我指路,将不胜感激。

EDIT编辑


I've come to this implentation myself, though it is not working... The result bitmap is transparent and empty.我自己已经实现了这个,虽然它不起作用......结果 bitmap 是透明和空的。

private string ConvertSVGToPNGRectangleWithSkiaSharpExtended(string path, double left, double top, double right, double bottom)
{
    var svg = new SkiaSharp.Extended.Svg.SKSvg();
    var picture = svg.Load(path);

    // Get the initial map size
    var source = new SKRect(picture.CullRect.Left, picture.CullRect.Top, picture.CullRect.Width, picture.CullRect.Height);

    // Cropping Rect
    var cropRect = new SKRect((int)left, (int)top, (int)right, (int)bottom);

    var croppedBitmap = new SKBitmap((int)cropRect.Width, (int)cropRect.Height);

    using var canvas = new SKCanvas(croppedBitmap);
    canvas.Clear(SKColors.Transparent);
    canvas.DrawBitmap(croppedBitmap, source, cropRect);

    var data = croppedBitmap.Encode(SKEncodedImageFormat.Png, 100);

    path = Path.ChangeExtension(path, "PNG");
    using var imageStream = new FileStream(path, FileMode.Create);
    data.SaveTo(imageStream);

    return path;
}

Disclaimer: I'm not an expert with SkiaSharp, but I do know about drawing and canvases and I looked up the documentation at https://docs.microsoft.com/en-us/dotnet/api/skiasharp.skcanvas.drawpicture免责声明:我不是 SkiaSharp 的专家,但我确实知道绘图和画布,我在https://docs.microsoft.com/en-us/dotnet/api/skiasharp.skcanvas.drawpicture上查找了文档

I think the problem is this line:我认为问题在于这一行:

canvas.DrawBitmap(croppedBitmap, source, cropRect);

I think you need:我认为你需要:

canvas.DrawPicture(picture, -cropRect.Left, -cropRect.Top);
canvas.Flush();

I might have the coordinates wrong, but the problem behind your failure is that you are never drawing picture to the canvas.我的坐标可能有误,但您失败的问题是您从未将picture绘制到 canvas。

To help with your confusion about what a canvas is: SKCanvas(croppedBitmap) is making it so that every time you draw to canvas , what you are actually drawing on is the bitmap.为了帮助您消除对 canvas 是什么的困惑: SKCanvas(croppedBitmap)正在制作它,以便每次您绘制到canvas时,您实际绘制的是 Z86BB33755628454AF74F88F047EC8 So in order to draw your picture onto the bitmap, you have to draw picture onto canvas .因此,为了将您的图片绘制到 bitmap 上,您必须将picture绘制到canvas上。 Using negative coordinates make it so the top and left of the svg picture are above and to the left of the top-left corner of the bitmap, which is the equivalent of having the bitmap start somewhere in the middle of the svg picture. Using negative coordinates make it so the top and left of the svg picture are above and to the left of the top-left corner of the bitmap, which is the equivalent of having the bitmap start somewhere in the middle of the svg picture.

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM