Problem with SVG loading

Topics: SVG, SVG# and Parsing, WPF and GDI+ Renderers
May 4, 2011 at 8:04 AM
Edited May 4, 2011 at 10:13 AM

Hi everybody

I've a big problem with my SVG files: I use the Sharpvectors library to load and display svg files on a WPF canvas.
Now the problem is, every displayed SVG file hasn't the defined pagesize, but has exactly the size of the content. The placement of the path on the page are completely ignored.
When I place a transparent rectangle with the size of the page, everything works fine, but this can't be the solution?!

What I'm doing: I use the 'Read' method of the FileSvgReader-Class, create a DrawingImage of it and set it as a ImageSource of an Image-object. This image is placed at a specific point with Canvas.SetLeft and .SetTop on my canvas.

Any help is appreciated.


May 5, 2011 at 6:12 AM

Hello Dhafner,
One problem I have had with WPF when working on this library is how WPF evaluates size or dimensions,
it turns out be limited to regions where there is "real" drawing, not my expected bounds.

I do not know if that is the same problem with your case, but one way I resolved this is to set background color
of the canvas, even transparent color was working.

Also, try using the SvgCanvas provided with the library and see if there any difference in the output.

Please let me know the results.

Best regards,

May 5, 2011 at 6:59 AM
Edited May 5, 2011 at 7:01 AM

Hi Paul

Thanks for your reply.
I spend some time with debugging the SharpVectors libraries and found a solution:

I created a class SvgImageWithOffset which inherits System.Windows.Controls.Image and added a property called Offset. The property is filled with the value from DrawingGroup.Bounds.Location. This Point seems to be the correct offset of the content compared to the pagesize.
When I place my image on the canvas, i simply add the x and y offset-values to the desired position and everything looks fine.

var reader = new FileSvgReader(false, false, new DirectoryInfo("C:\\temp"), settings);
var drawgroup = reader.Read(file);
ImageSource source = new DrawingImage(drawgroup);
var img = new SvgImageWithOffset
       Source = source,
       Offset = drawgroup.Bounds.Location
Canvas.SetLeft(img, 123 + img.Offset.X);
Canvas.SetTop(img, 321 + img.Offset.Y);

I tried the SvgDrawingCanvas, but with the code above, no image is displayed. Strange, because the SvgDrawingCanvas inherits the normal canvas...

Regards from Switzerland
May 5, 2011 at 8:33 AM
Edited May 5, 2011 at 8:34 AM

Hello Dhafner,

Thanks for the input. Nice to know you have found a solution.
Now, I see the problem you are trying to solve.

If you look at the sources of the SvgDrawingCanvas class, I did something similar with
_offsetX and _offsetY members. Unfortunately, this does not always work, so I stopped
using it. When using SvgDrawingCanvas, you will have to use the DrawingGroup not DrawingImage,
specifically, use the SvgDrawingCanvas.LoadDiagrams(string fileName) method. 

Currently, I think rendering the drawing directly in the OnRender method and applying translation
transformation could be the most reliable to avoid this problem.

This is done by similar SVG project on Codeproject, and it seems to be the best approach, still evaluating
this option, please take a look if you encounter further problem

Once stopped over at Switzerland (on my way to Japan), it is a beautiful country :)


Best regards,


May 5, 2011 at 8:54 AM
Edited May 5, 2011 at 9:00 AM

Hi Paul

I have managed to display something on the SvgDrawingCanvas, but the same problem has occurred: The offset is ignored.

While I played around with my solution and some test SVGs, i didn't experienced, that the placement is wrong. But i'll keep your solution in mind.

Thanks a lot for your help, keep up the good work on this project.
It's far the best way I've found on the net to handle SVGs in a WPF application.


May 5, 2011 at 9:08 AM

Hello Daniel,

Thanks for the information and the supports, and finally giving your name :)

I will look into this issue to find a working solution, so that the drawing canvas can be
useful to those wishing to use it, sorry for any inconvenience.

Best regards,