C# .svg file to System.Drawing.Image
Asked Answered
T

3

7

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.

Tanya answered 20/2, 2016 at 13:39 Comment(1)
How should i use SVGParser ? is there any nuget ??Compress
S
-4

So,

SVGParser.MaximumSize = new System.Drawing.Size(4000, 4000);
svgDocument = SVGParser.GetSvgDocument(mPath);
var bitmap = svgDocument.Draw();
image = bitmap;
Swetiana answered 20/2, 2016 at 13:53 Comment(3)
How should i use SVGParser ? is there any nuget ??Compress
You should place a link to the library you are using on the example because without it the answer is quite uselessStardom
He used a package called svg. To install it simply run the following in the package manager: Install-Package SvgPersuasive
S
5

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.

Stardom answered 20/3, 2017 at 14:7 Comment(4)
How do you get this to work in a loop and not trash the 2nd and subsequent images? Do you know?Lordan
@Lordan what do you mean by trash the 2nd ? You could try to put the svgDoc into an using like the ImageStardom
what is context in your example? it's not declaredRempe
@Rempe the context is the HttpContext, i'm using this inside a handler, so the context is given as a parameter on the ProcessRequest methodStardom
C
0

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.

Comnenus answered 14/5, 2021 at 10:11 Comment(0)
S
-4

So,

SVGParser.MaximumSize = new System.Drawing.Size(4000, 4000);
svgDocument = SVGParser.GetSvgDocument(mPath);
var bitmap = svgDocument.Draw();
image = bitmap;
Swetiana answered 20/2, 2016 at 13:53 Comment(3)
How should i use SVGParser ? is there any nuget ??Compress
You should place a link to the library you are using on the example because without it the answer is quite uselessStardom
He used a package called svg. To install it simply run the following in the package manager: Install-Package SvgPersuasive

© 2022 - 2024 — McMap. All rights reserved.