<p>I've been trying to convert SVG images to PNG using C#, without having to write too much code. Can anyone recommend a library or example code for doing this?</p>

[{'answer_id': 58912, 'author': 'Espo', 'author_id': 2257, 'author_profile': 'https://Stackoverflow.com/users/2257', 'pm_score': 7, 'selected': True, 'text': '<p>You can call the command-line version of inkscape to do this:</p>\n\n<p><a href="http://harriyott.com/2008/05/converting-svg-images-to-png-in-c.aspx" rel="noreferrer">http://harriyott.com/2008/05/converting-svg-images-to-png-in-c.aspx</a></p>\n\n<p>Also there is a C# SVG rendering engine, primarily designed to allow SVG files to be used on the web on codeplex that might suit your needs if that is your problem:</p>\n\n<p><strong>Original Project</strong><br>\n<a href="http://www.codeplex.com/svg" rel="noreferrer">http://www.codeplex.com/svg</a></p>\n\n<p><strong>Fork with fixes and more activity:</strong> (added 7/2013)<br>\n<a href="https://github.com/vvvv/SVG" rel="noreferrer">https://github.com/vvvv/SVG</a></p>\n'}, {'answer_id': 548553, 'author': 'stevenvh', 'author_id': 66056, 'author_profile': 'https://Stackoverflow.com/users/66056', 'pm_score': 3, 'selected': False, 'text': '<p>I\'m using <a href="http://xmlgraphics.apache.org/batik/" rel="nofollow noreferrer">Batik</a> for this. Batik is a graphics library written in Java, with a command line interface. This makes that you can Batik from C#, same as the following example in Delphi:</p>\n<pre><code>procedure ExecNewProcess(ProgramName : String; Wait: Boolean);\nvar\n  StartInfo : TStartupInfo;\n  ProcInfo : TProcessInformation;\n  CreateOK : Boolean;\nbegin\n  FillChar(StartInfo, SizeOf(TStartupInfo), #0);\n  FillChar(ProcInfo, SizeOf(TProcessInformation), #0);\n  StartInfo.cb := SizeOf(TStartupInfo);\n  CreateOK := CreateProcess(nil, PChar(ProgramName), nil, nil, False,\n              CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS,\n              nil, nil, StartInfo, ProcInfo);\n  if CreateOK then begin\n    //may or may not be needed. Usually wait for child processes\n    if Wait then\n      WaitForSingleObject(ProcInfo.hProcess, INFINITE);\n  end else\n    ShowMessage(\'Unable to run \' + ProgramName);\n\n  CloseHandle(ProcInfo.hProcess);\n  CloseHandle(ProcInfo.hThread);\nend;\n\nprocedure ConvertSVGtoPNG(aFilename: String);\nconst\n  ExecLine = \'c:\\windows\\system32\\java.exe -jar C:\\Apps\\batik-1.7\\batik-rasterizer.jar \';\nbegin\n  ExecNewProcess(ExecLine + aFilename, True);\nend;\n</code></pre>\n'}, {'answer_id': 583746, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': -1, 'selected': False, 'text': '<p>you can use altsoft xml2pdf lib for this</p>\n'}, {'answer_id': 7003793, 'author': 'nw.', 'author_id': 307960, 'author_profile': 'https://Stackoverflow.com/users/307960', 'pm_score': 4, 'selected': False, 'text': '<p>When I had to rasterize svgs on the server, I ended up using P/Invoke to call librsvg functions (you can get the dlls from a windows version of the GIMP image editing program).</p>\n\n<pre><code>[DllImport("kernel32.dll", SetLastError = true)]\nstatic extern bool SetDllDirectory(string pathname);\n\n[DllImport("libgobject-2.0-0.dll", SetLastError = true)]\nstatic extern void g_type_init(); \n\n[DllImport("librsvg-2-2.dll", SetLastError = true)]\nstatic extern IntPtr rsvg_pixbuf_from_file_at_size(string file_name, int width, int height, out IntPtr error);\n\n[DllImport("libgdk_pixbuf-2.0-0.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]\nstatic extern bool gdk_pixbuf_save(IntPtr pixbuf, string filename, string type, out IntPtr error, __arglist);\n\npublic static void RasterizeSvg(string inputFileName, string outputFileName)\n{\n    bool callSuccessful = SetDllDirectory("C:\\\\Program Files\\\\GIMP-2.0\\\\bin");\n    if (!callSuccessful)\n    {\n        throw new Exception("Could not set DLL directory");\n    }\n    g_type_init();\n    IntPtr error;\n    IntPtr result = rsvg_pixbuf_from_file_at_size(inputFileName, -1, -1, out error);\n    if (error != IntPtr.Zero)\n    {\n        throw new Exception(Marshal.ReadInt32(error).ToString());\n    }\n    callSuccessful = gdk_pixbuf_save(result, outputFileName, "png", out error, __arglist(null));\n    if (!callSuccessful)\n    {\n        throw new Exception(error.ToInt32().ToString());\n    }\n}\n</code></pre>\n'}, {'answer_id': 12884409, 'author': 'Anish', 'author_id': 463469, 'author_profile': 'https://Stackoverflow.com/users/463469', 'pm_score': 6, 'selected': False, 'text': '<p>There is a much easier way using the library <a href="http://svg.codeplex.com/">http://svg.codeplex.com/</a> (Newer version @<a href="https://github.com/vvvv/SVG">GIT</a>, @<a href="https://www.nuget.org/packages/Svg/">NuGet</a>).  Here is my code</p>\n\n<pre><code>var byteArray = Encoding.ASCII.GetBytes(svgFileContents);\nusing (var stream = new MemoryStream(byteArray))\n{\n    var svgDocument = SvgDocument.Open(stream);\n    var bitmap = svgDocument.Draw();\n    bitmap.Save(path, ImageFormat.Png);\n}\n</code></pre>\n'}, {'answer_id': 14878965, 'author': 'Michal Kieloch', 'author_id': 936038, 'author_profile': 'https://Stackoverflow.com/users/936038', 'pm_score': 2, 'selected': False, 'text': '<p>To add to the response from @Anish, if you are having issues with not seeing the text when exporting the SVG to an image, you can create a recursive function to loop through the children of the SVGDocument, try to cast it to a SvgText if possible (add your own error checking) and set the font family and style.</p>\n\n<pre><code>    foreach(var child in svgDocument.Children)\n    {\n        SetFont(child);\n    }\n\n    public void SetFont(SvgElement element)\n    {\n        foreach(var child in element.Children)\n        {\n            SetFont(child); //Call this function again with the child, this will loop\n                            //until the element has no more children\n        }\n\n        try\n        {\n            var svgText = (SvgText)parent; //try to cast the element as a SvgText\n                                           //if it succeeds you can modify the font\n\n            svgText.Font = new Font("Arial", 12.0f);\n            svgText.FontSize = new SvgUnit(12.0f);\n        }\n        catch\n        {\n\n        }\n    }\n</code></pre>\n\n<p>Let me know if there are questions.</p>\n'}]
['https://Stackoverflow.com/questions/58910', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5744/']