This project has moved. For the latest updates, please go here.

Convert SVG to PNG (or any other format) does not apply preserveAspectRatio

Sep 5, 2013 at 3:09 PM
Hi

I'm using Magick.NET-Q16-x64 v 6.8.6.801 from NuGet.
.Net 4.5 (VS2012).

I need to convert a SVG image to one of the raster formats (for example PNG).
I've checked the documentation and tried the code which perform convertation and all works fine (at least at first sight). Except one thing - preserveAspectRatio. I use next value preserveAspectRatio="xMinYMin slice"; but the resulting image looks like the value is preserveAspectRatio="xMinYMin meet" or like there is no preserveAspectRatio at all.

One more detail is that svg file contain linked raster image which is wrapped with svg element with preserveAspectRatio attribute so this attribute has affect.

Does Magic.NET support preserveAspectRatio attribute?
And if yes how I can make it work?

Below is svg sample code which I tested. Open it in browser and then compare to the converted with Magic.NET PNG.
<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<svg width="460px" height="425px" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
 <g>
  <title>Layer 1</title>
  <svg viewBox="0 0 220 300" x="0" y="0" width="460" height="425" preserveAspectRatio="xMinYMin slice">
   <image id="svg_1" height="425px" width="460px" xlink:href="http://viewallpaper.com/wp-content/uploads/2013/07/Images-Water-Wallpaper.jpg" y="0" x="0" preserveAspectRatio="xMinYMin slice"/>
  </svg>
 </g>
</svg>
Looking forward for the response/advice and thanks in advance.
Coordinator
Sep 5, 2013 at 3:55 PM
I just checked the ImageMagick source and I am unable to find preserveAspectRatio in the svg reader. The support for reading SVG files is not that good in ImageMagick. For Linux they recommend using inkscape (http://inkscape.org) and there is a download available for Windows. The only problem with this is that they use GPL2 instead of the apache license. If you are not going to use this in a commercial application that should not be a problem.
Sep 5, 2013 at 4:44 PM
Edited Sep 5, 2013 at 4:46 PM
Thanks for quick replay.

I have tried inkscape and it works fine (with preserveAspectRatio).
I have installed ImageMagick-6.8.6-Q16 x64 and Inkscape and when I run convert test.svg test.png from command line in ImageMagick folder the resulting raster image is good. I know that ImageMagick will use Inkscape for reading SVG if it is installed and it use it indeed but only from ImageMagick command line.

I thought that Magick.NET API will also use Inkscape for reading SVG, but when I run code that use Magick.NET to convert SVG to raster image, the result is the same - no preserveAspectRatio.

Am I wrong and Magick.NET will not use Inkscape for reading SVG or I'm doing something wrong in code?
Coordinator
Sep 5, 2013 at 8:05 PM
The location of inkscape is probably not added to your %PATH%. You will get more detailed information when you use the SetLogEvents method of the MagickNET class. You should probably also set the Debug property of your MagickImage to true.
Sep 6, 2013 at 1:55 PM
It works.

Thank you.