10

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?

Scott
  • 13,735
  • 20
  • 94
  • 152
  • http://imagemagick.codeplex.com/ might be useful. – MGwynne Jun 06 '11 at 15:41
  • did u resoleve ur issue? would u whare with us plz? – Armance Dec 05 '11 at 12:18
  • No, never did. We abandoned that approach. – Scott Dec 06 '11 at 18:54
  • I also am looking for approaches to convert an SVG created in Inkscape to Png or Jpeg. Currently I'm using the approach listed here http://stackoverflow.com/questions/58910/converting-svg-to-png-using-c-sharp specifically using DLL's and pinvokes for libs that ship with The Gimp. – Frank Hale Dec 13 '11 at 22:06

3 Answers3

7

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!

Daniel
  • 9,312
  • 3
  • 48
  • 48
4

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.

4

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.

keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • 2
    thanks ,im trying to use ur suggestion but i get error `Object reference not set to an instance of an object` could u [help plz](http://stackoverflow.com/questions/8414324/convert-svg-to-image-programatically) – Armance Dec 07 '11 at 11:29
  • I am also getting the "Object reference not set to an instance of an object" error when I call the Draw() method. – Frank Hale Dec 13 '11 at 22:04
  • @FrankHale - Did the solution in astrocybernaute's link work for you? – keyboardP Dec 13 '11 at 23:46
  • 1
    The SVG Rendering Engine code does not work for me. I get the object reference error when I try to call Draw(). I'm currently using a method with some pinvokes from libs that are installed with The Gimp. – Frank Hale Dec 14 '11 at 03:29
  • I know my SVG is well formed. I have no issues rendering it or using libs from The Gimp. I don't know what is going on with the SVG Rendering Engine method. – Frank Hale Dec 14 '11 at 03:30
  • @FrankHale - It'll probably be good idea to start a new question as I've never used the library myself. Maybe someone else will know. To ensure people don't claim it to be a duplicate, mention that you've read this question and the link in `astrocybernaute's` comment above. – keyboardP Dec 14 '11 at 04:19
  • Unfortunately, it does not work in asp.net core 2 and the following error occurs Reference to type 'Bitmap' claims it is defined in 'System.Drawing', but it could not be found and Reference to type 'Graphics' claims it is defined in 'System.Drawing', but it could not be found – mohamad javad Taherian Mar 26 '18 at 09:54