This project is read-only.

Convert SVG to PNG with Images Embeded

Aug 5, 2013 at 2:38 AM
I've managed to take a svg from my server and save it back to the server as a png using this engine and the code
    Dim sampleDoc = SvgDocument.Open(filePath)
    sampleDoc.Draw().Save(exportPath, Drawing.Imaging.ImageFormat.Png)
However the <image> elements inside the SVG aren't being rendered.
The images originally had their xmlns:xlink value set to a url of the images. I have also tried converting the images to string so that they are embedded inside the SVG like so

Am I doing something wrong or are images elements not supported when converting to raster files?
Sep 2, 2013 at 8:34 AM
I got the same issue too.

My best guess is that this library doesn't support embedding images either through external links or even in base64 format.

Can someone who has gone through the code confirm this?
Oct 24, 2013 at 5:00 PM
Can anyone familiar with this library comment on this? I'm running into the same problem, and find it hard to believe that there is zero support for linked images...
Oct 29, 2013 at 10:08 PM
I am not familiar with this library but I just had a look at the source code

On line 96 it has a comment which says TO DO, in the Render override, therefore I think it's safe to assume that it has not been done yet!
May 30, 2014 at 5:03 PM
Actually, the new svg library for 1.6.1 has all the logic inbuilt for rendering the images while converting the svg to image format. We had the same issue for converting the svg to image. Initially we used svg 1.5 library which doesn't support rendering images embedded inside image tag, but svg 1.6.1 does support the same. Hope this helps.
Jul 20, 2015 at 7:42 PM
Edited Jul 20, 2015 at 8:40 PM
I am using Svg.dll version and have added the Svg.dll to the project as a reference.

When I try to call:
Svg.SvgDocument svgDoc = Svg.SvgDocument.Open(inFile);
SvgDoc.Draw().Save(outFile, System.Drawing.Imaging.ImageFormat.Png);

I get prompted to open SvgUnit.cs or other Svg .cs files sicj as SvgDocument.cs. Any ideas?

and when I click cancel it results in a "Process is terminated due to StackOverflowException"
Jul 21, 2015 at 11:40 PM
To success view the embedded images you have to remove the line 254 (stream.Position = 0) of the file SvgImage.cs because WebResponse doesnt support positioning.
using (WebResponse webResponse = httpRequest.GetResponse())
                    using (var stream = webResponse.GetResponseStream())
                        //stream.Position = 0;
                        if (uri.LocalPath.EndsWith(".svg", StringComparison.InvariantCultureIgnoreCase))
                            var doc = SvgDocument.Open<SvgDocument>(stream);
                            doc.BaseUri = uri;
                            return doc;
                            return Bitmap.FromStream(stream);
After that the embedded image works correctly.

I hope it is useful
Jul 22, 2015 at 2:59 PM
Edited Jul 22, 2015 at 3:00 PM
Are you referring to the Svg.dll version Here?
Jul 22, 2015 at 3:18 PM
Edited Jul 22, 2015 at 3:32 PM
And now it generates the StackOverflowException error in a different location:
Which seems to differ each time:
       private IFontDefn GetFont(ISvgRenderer renderer, SvgElement owner)
            if (owner == null) return null;

            var visual = owner.ParentsAndSelf.OfType<SvgVisualElement>().FirstOrDefault(); <--here once
            return visual.GetFont(renderer);
and then here:
        /// <summary>
        /// Gets the attribute with the specified name and inherits from ancestors if there is no attribute set.
        /// </summary>
        /// <typeparam name="TAttributeType">The type of the attribute value.</typeparam>
        /// <param name="attributeName">A <see cref="string"/> containing the name of the attribute.</param>
        /// <returns>The attribute value if available; otherwise the ancestors value for the same attribute; otherwise the default value of <typeparamref name="TAttributeType"/>.</returns>
        public TAttributeType GetInheritedAttribute<TAttributeType>(string attributeName)
        { <-- error generated here
            if (this.ContainsKey(attributeName) && !IsInheritValue(base[attributeName]))
                var result = (TAttributeType)base[attributeName];
                var deferred = result as SvgDeferredPaintServer;
                if (deferred != null) deferred.EnsureServer(_owner);
                return result;

            if (this._owner.Parent != null)
                if (this._owner.Parent.Attributes[attributeName] != null)
                    return (TAttributeType)this._owner.Parent.Attributes[attributeName];
Jul 24, 2015 at 10:15 PM
Edited Jul 24, 2015 at 10:16 PM
Sorry, I download for mistake an old version and I suppose was the latest. This fix was already implemented in the last version.

I try this version and it works with linked images.

holzert: I don't have the exceptions you found. Do you try different embedded images ?
Jul 25, 2015 at 5:37 PM
Thanks for testing! The image I posted worked for you? These are diagrams where those images need to work. It produces the same errors even a separate test windows service or app I created outside of our windows service. I tried in two versions - the earlier version produced black images and the newest version gets into the stackoverflow error everytime - even with smaller images that I did try.