This project has moved and is read-only. For the latest updates, please go here.

Stream Converter fails, would appreciate help.

Mar 17, 2011 at 7:59 PM
Edited Mar 17, 2011 at 8:01 PM

Hi guys,

I'm not looking to render the SVG file to a WPF, but the output should be a PNG or JPG that can be sent back to a client browser. In my project, I take a string input which contains SVG XML, and I want to to rasterize it to a bitmap. The converter seems to fail when trying to convert from an stream containing the svg text into an image stream. So the code looks like this:

(For formatting reasons, string svg is broken up into multiple lines. If you try this, you should combine the lines into a single declaration.)

            string svg = "<svg width=\"800\" height=\"600\">
  <path stroke-width=\"2\" id=\"svg_102\" d=\"m9.875,23c-2,-4.25 -1.6875,-7.375 1.6875,-10.5c3.375,-3.125 7.5625,-2.75 11.0625,2\"
stroke=\"#8dd35f\" fill=\"none\"/>
  <line fill=\"none\" stroke=\"#606060\" id=\"svg_109\" y2=\"4\" x2=\"19\" y1=\"19\" x1=\"4\"/>
  <circle stroke=\"#0000ff\" fill=\"#00ffff\" id=\"svg_111\" r=\"2.17578\" cy=\"11.5\" cx=\"11.5\"/>
  <circle stroke-width=\"0.5\" id=\"svg_121\" stroke=\"#0000ff\" fill=\"#00ffff\" r=\"2.26172\" cy=\"4\" cx=\"19\"/>
  <circle id=\"svg_123\" stroke-width=\"0.5\" stroke=\"#0000ff\" fill=\"#00ffff\" r=\"2.26172\" cy=\"19\" cx=\"4\"/>
</svg>";
byte[] svgArray = Encoding.UTF8.GetBytes(svg);

using (var svgStringSteam = new MemoryStream(svgArray))
{
WpfDrawingSettings settings = new WpfDrawingSettings
{
IncludeRuntime = true,
TextAsGeometry = true,
};

var converter = new StreamSvgConverter(settings);

using (var imageStream = new MemoryStream())
{
if (converter.Convert(svgStringSteam, imageStream))
{

var image = Image.FromStream(imageStream);
image.Save(imageStream, System.Drawing.Imaging.ImageFormat.Png);

return new FileContentResult(imageStream.ToArray(), "image/png");
}
}
}
Mar 18, 2011 at 6:31 PM
Edited Mar 18, 2011 at 6:37 PM

I think I can answer my own question. When using the stream converters, it appears two options must be set in the StreamSvgConverter object, otherwise, Sharp Vectors will return false. "SaveXaml" and SaveZaml" must be true.

Corrected code:

            WpfDrawingSettings settings = new WpfDrawingSettings
            {
                IncludeRuntime = true,
                TextAsGeometry = false
            };

            var converter = new StreamSvgConverter(settings) {SaveXaml = true, SaveZaml = true};

            using (var svgStringSteam = new MemoryStream(Encoding.Default.GetBytes(svg)))
            {
                using (var imageStream = new MemoryStream())
                {
                    if (converter.Convert(svgStringSteam, imageStream))
                    {
                        //converter.Convert(@"C:\svg.svg", imageStream);
                        var image = Image.FromStream(imageStream);
                        image.Save(imageStream, ImageFormat.Png);

                        Response.AddHeader("Content-Disposition", "attachment; filename=\"filenamehere.png\""); 
                        return File(imageStream.ToArray(), "image/png");
                    }
                }
            }
Coordinator
Mar 18, 2011 at 6:49 PM

Sorry, that is a bug.

Only the following overload

public bool Convert(string svgFileName, Stream imageStream)

is correctly implemented, and that does required SaveXaml=true, SaveZaml=true.

Best regards,
Paul.