Vulovic Vukasin
Vulovic Vukasin

Reputation: 1748

C# .svg file to System.Drawing.Image

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.

Upvotes: 8

Views: 33907

Answers (3)

Ariel
Ariel

Reputation: 916

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.

Upvotes: 5

Dikkie Dik
Dikkie Dik

Reputation: 31

The resource Miljan Vulovic used is svg (https://archive.codeplex.com/?p=svg).

Link is only valid until July 2021, it might be available on GitHub by then, but I'm not sure.

Anyways his solution works for me.

Upvotes: 0

Miljan Vulovic
Miljan Vulovic

Reputation: 1636

So,

SVGParser.MaximumSize = new System.Drawing.Size(4000, 4000);
svgDocument = SVGParser.GetSvgDocument(mPath);
var bitmap = svgDocument.Draw();
image = bitmap;

Upvotes: -3

Related Questions