Click here to Skip to main content
14,605,926 members

SVGImage Control

Rate this:
4.88 (43 votes)
Please Sign up or sign in to vote.
4.88 (43 votes)
29 Jul 2010CPOL
A WPF control for showing SVG images.



SVG (Scalable Vector Graphic) is an XML based graphic format.

This little project started out as a simple SVG to XAML converter. I looked at some simple SVG files and noticed the similarity to XAML defined graphic, and decided to write a converter. However, I soon realized that SVG is a very complex format, and at the same time, I found no good reason for converting to XAML just to show the image on the screen, so instead, I started working on the SVG, SVGRender and the SVGImage classes.

  • The SVG class is the class that reads and parses the XML file.
  • SVGRender is the class that creates the WPF Drawing object based on the information from the SVG class.
  • SVGImage is the image control. The image control can either load the image from a filename - SetImage(filename) - or by setting the Drawing object through SetImage(Drawing), which allows multiple controls to share the same drawing instance.

The control only has a couple of properties, SizeType and ImageSource.

  • SizeType - controls how the image is stretched to fill the control
    • None: The image is not scaled. The image location is translated so the top left corner of the image bounding box is moved to the top left corner of the image control.
    • ContentToSizeNoStretch: The image is scaled to fit the control without any stretching. Either X or Y direction will be scaled to fill the entire width or height.
    • ContentToSizeStretch: The image will be stretched to fill the entire width and height.
    • SizeToContent: The control will be resized to fit the un-scaled image. If the image is larger than the max size for the control, the control is set to max size and the image is scaled.

    For None and ContentToSizeNoStretch, the Horizontal/VerticalContentAlignment properties can be used to position the image within the control.

  • ImageSource - This property is the same as SetImage(drawing), and is exposed to allow for the source to be set through binding.

The SVG format

Before writing any parser, it is a good idea to try and get a good understanding of the format you are going to parse, so I looked around for some documentation and came across this link which is the specification for SVG:

I also found this online book (also available for download in PDF format):

and this free SVG editor:

And after spending some time reading, I was ready to get started on the parser.

A quick walkthrough of the parsing

The main entry point is the SVG constructor (SVGReader.cs) which takes a filename. The only point of interest here is that I had to set the XmlResolver to null to avoid getting exceptions while reading different SVG files I found online.

doc.XmlResolver = null; 

Each XML node is then parsed in the static method Group.AddToList (Shape.cs). Here, a new Shape object is created based on the XML tag, and the object itself is then passed the XML node to complete the parsing.

All basic shapes are implemented in Shapes.cs, while the more complex Path shape is implemented in PathShape.cs.

Finally, the actual WPF Drawing object is generated in the SVGRender class.

SVGRender.LoadGroup creates a single DrawingGroup object which contains a child group for each of the SVG shapes, where each shape is converted into a Geometry and then wrapped in a GeometryDrawing object to set the fill and stroke style.

That is pretty much it.


7/29/2010 - added basic support for tspan in the text element.

Not all the different attributes supported by tspan are implemented, only font, style, and stroke/fill color.

Each tspan element can be thought of as an individual text element, complete with style, stroke, and fill attributes, and for this reason, it is derived from the base shape.

Parsing of a text with tspan elements is done in TextShape.ParseTSpan.

The rendering part is not designed for nested shapes (except for groups), but expect one shape, one geometry. TSpan is a bit different; each tspan element (even if nested) is added to the same GeometryGroup when the geometries are build. Each geometry can have different attributes which are the attributes on the tspan shape itself. Instead of redesigning how the SVGRender creates the drawing by iterating though the shapes, I chose to attach the tspan element (shape) itself directly to each of the build geometries using attached properties and then 'extract' the shape from the geometry when creating the drawing.

if (shape is ClipArtViewer.TextShape)
       GeometryGroup gp = TextRender.BuildTextGeometry(shape as ClipArtViewer.TextShape);
       if (gp != null)
              foreach (Geometry gm in gp.Children)
                     TextShape.TSpan.Element tspan = TextRender.GetElement(gm);
                     if (tspan != null)
                           grp.Children.Add(NewDrawingItem(tspan, gm));
                           grp.Children.Add(NewDrawingItem(shape, gm));


The SVG parser and render is still not complete, but it is in a state where it can read most 'simple' SVG files I have found online so far. Of the chapters in the SVG Essentials book, the following chapters have been implemented:

  • Chapter 3 - Basic shapes
  • Chapter 5 - Transforming the coordinate system
  • Chapter 6 - Path
  • Chapter 7 - Patterns and gradients (only gradients is implemented)

I will continue working on this, and the next step is probably to add support for the text element.

Note! Some of the SVG files in the sample folder were found as free files online.

Revision history

  • 7/9/10 - Added support for <use> and <image> tags. Only internal groups can be referenced by <use>. The filename referenced by <image> must be relative to the current location of the CSV file. Examples 4.5, 4.6, and 4.8 from SVG Essentials have been added.
  • svgimage_02.jpg

  • 7/11/10 - Added support for simple text and formatting. Still missing support for word / letter spacing, tspan, text along a path, and other advanced format options.
  • svgimage_03.jpg

  • 7/13/10 - Uploaded new zip file with the TestForm file removed.
  • 7/18/10 - Added support for letter spacing and word spacing.
  • svgimage_04.jpg

    For text formatting, I had to use the low level GlyphRun, as FormattedText does not provide the same control over letter and word spacing.

    I added a new class TextRender.BuildTextGeometry (in TextRender.cs) which builds the geometry for the text using a GlyphRun example I found in this free eBook:, with some slight modifications to fit my needs.

  • 7/29/2010
  • svgimage_05.jpg

    I finally got around to add basic support for tspan in the text element. Not all the different attributes supported by tspan are implemented, only font, style, and stroke/fill color.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
United States United States
No Biography provided

Comments and Discussions

AnswerRe: Any plan for a SVG Editor? Pin
Bill Seddon13-Jul-10 0:42
MemberBill Seddon13-Jul-10 0:42 
GeneralRe: Any plan for a SVG Editor? Pin
jkristia13-Jul-10 3:05
Memberjkristia13-Jul-10 3:05 
GeneralRe: Any plan for a SVG Editor? Pin
JohnWindy13-Jul-10 22:05
MemberJohnWindy13-Jul-10 22:05 
QuestionLink image is missing? Pin
JohnWindy9-Jul-10 2:37
MemberJohnWindy9-Jul-10 2:37 
AnswerRe: Link image is missing? [modified] Pin
jkristia9-Jul-10 3:31
Memberjkristia9-Jul-10 3:31 
GeneralRe: Link image is missing? Pin
JohnWindy11-Jul-10 22:27
MemberJohnWindy11-Jul-10 22:27 
AnswerRe: Link image is missing? Pin
flanc16-Nov-14 16:15
Memberflanc16-Nov-14 16:15 
GeneralMy vote of 5 Pin
geo_m8-Jul-10 1:20
Membergeo_m8-Jul-10 1:20 
Thank you!
GeneralBug Pin
reflex@codeproject7-Jul-10 8:40
Memberreflex@codeproject7-Jul-10 8:40 
GeneralRe: Bug Pin
jkristia7-Jul-10 9:52
Memberjkristia7-Jul-10 9:52 
GeneralRe: Bug Pin
reflex@codeproject7-Jul-10 11:24
Memberreflex@codeproject7-Jul-10 11:24 
GeneralRe: Bug Pin
jkristia7-Jul-10 15:24
Memberjkristia7-Jul-10 15:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 7 Jul 2010

Tagged as


135 bookmarked