Convert SVG to PNG or JPEG

2019-02-12 17:39发布

What methods currently exist to convert an SVG image to PNG or JPEG programmatically using C#?

I've read all of the existing SO questions on this topic, and all of them involve using an external process to launch a third party program. In my case, this isn't an option as we'll be migrating to Azure soon.

What I need to be able to do is to load the SVG file from disk and ideally convert it to something I can use the System.Drawing classes to manipulate.

Any ideas?

标签: c# image svg
3条回答
手持菜刀,她持情操
2楼-- · 2019-02-12 17:42

You could take a look at SVG Rendering Engine on CodePlex. It has an overload that will take an in-memory stream (your SVG) which can then be used to convert to an image.

查看更多
迷人小祖宗
3楼-- · 2019-02-12 17:48

All you need to do is installing SVG Rendering Library nuget package.

Install-Package Svg

And then

        //read svg document from file system
        var svgDocument = SvgDocument.Open("test.svg");
        var bitmap = svgDocument.Draw();
        //save converted svg to file system
        bitmap.Save("test.png", ImageFormat.Png);

That's it.

查看更多
Melony?
4楼-- · 2019-02-12 18:02

Well. I will share with my solution to render a SVG file resized to a proper size.

I install this nuget package

Install-Package Svg

You can find the package source code on github here

Then, you can do this:

var svgDocument = SvgDocument.Open(path);
using (var smallBitmap = svgDocument.Draw())
{
    var width = smallBitmap.Width;
    var height = smallBitmap.Height;
    if (width != 2000)// I resize my bitmap
    {
        width = 2000;
        height = 2000/smallBitmap.Width*height;
    }

    using (var bitmap = svgDocument.Draw(width, height))//I render again
    {
        bitmap.Save(pngPath, ImageFormat.Png);
    }
}

Enjoy!

查看更多
登录 后发表回答