Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# .svg file to System.Drawing.Image

Tags:

c#

.net

png

svg

I need to convert the selected .svg file to System.Drawing.Image object, so I can resize it and save it as .png. Can anyone help me with this?

Here is what I have so far:

Svg.SvgDocument svgDocument = SVGParser.GetSvgDocument(mPath);
image = svgDocument.Draw();

But it gives me out of memory error.

like image 596
Vulovic Vukasin Avatar asked Feb 20 '16 13:02

Vulovic Vukasin


1 Answers

You can use the SVG Rendering Engine Lib:

Install-Package Svg

It's quite easy to draw images using it:

var svgDoc = SvgDocument.Open(imagePath);

using(var Image = new Bitmap(svgDoc.Draw()))
{
    Image.Save(context.Response.OutputStream, ImageFormat.Png);
    context.Response.ContentType = "image/png";
    context.Response.Cache.SetCacheability(HttpCacheability.Public);
    context.Response.Cache.SetExpires(DateTime.Now.AddMonths(1));
}

In this example i'm using a handler to display the image on the browser but you can easily save it on some folder just by changing the first parameter of the Save method.

like image 77
Ariel Avatar answered Oct 05 '22 23:10

Ariel