Ab2d.ReaderSvg - svg and svgz importer

Ab2d.ReaderSvg is a class library that can be used to read svg and svgz (compressed svg) files and import its elements as WPF UIElements - Canvases, Paths and other shapes. Elements Stroke and Fill data are also imported.

Note that Ab2d.ReaderSvg cannot be used in Silverlight applications - but can be used in XBAP Web Browser application. For Silverlight use ViewerSvg and select export as Silverlight xaml in the export options.

Read svg file in XAML:

  • In project add reference to Ab2d.ReaderSvg
  • Add namespace definition into root xaml element (for example Window):
    xmlns:ab2d="clr-namespace:Ab2d.Controls;assembly=Ab2d.ReaderSvg"
  • Use Ab2d.Controls.SvgViewbox to read the svg file as Shapes - set Source property to svg file location.
  • Use Ab2d.Controls.SvgDrawing to read a metafile file as Drawings - set Source property to metafile location.

Source property can be uses just as Source of the Image control. Depending on the location and svg file build action use of the following patterns to load the svg file (see also ReaderSvgSample that is included in the download):

Location: samples folder in project, Build Action: Content
<ab2d:SvgViewbox Source="samples/birthday_cake.svg"/>

Location: samples folder in project, Build Action: Resource - embedded into assembly
<ab2d:SvgViewbox Source="samples/home6.svg"/>

Location: internet, Build Action: not included in project
<ab2d:SvgViewbox Source="http://www.myweb.com/images/tiger.svg"/>

Location: loose svg file on the local disk, Build Action: not included in the project
<ab2d:SvgViewbox Source="c:\temp\birthday_cake.svg"/>

Location: loose svg file in the application directory, Build Action: not included in the project
<ab2d:SvgViewbox Source="pack://siteoforigin:,,,/home11.svg"/>



Svg ResourceDictionaryWriter sample application

The prefered way of storing cliparts in WPF is to use ResourceDictionaries. To simplify creating ResourceDictionaries from svg files there is a ResourceDictionaryWriter sample application. It is available with full source code, so it can be fully customized.

More about usage of ResourceDictionares can be found on the Microsoft's MSDN.

The following screenshot shows SVG ResourceDictionaryWriter application with three svg files added to the ResourceDictionary (xaml of ResourceDictionary can be seen on the right):
Svg ResourceDictionaryWriter screenshot



ReaderSvg Sample application:
ReaderSvg Sample application (click to see larger image)   ReaderSvg Sample application (click to see larger image)
I think a good sample can best describe features of the library. So be sure to get a demo application that demonstrates how simple is to create great animated effects, scalable vector elements according to the window size, add triggers to some elements and more. The sample is available with full source code.

Also check out the Links section for links to free online svg libraries and svg editors.


New in version 3.1

  • Added support for all types of svg patterns:
    before only TexturePatterns were supported, now other types of patterns are supported as well. All patterns are now converted into VisualBrush.
    NOTE: Silverlight does not support VisualBrush - Red brush is used instead.
  • Added support for complex Strokes and Pens - before Strokes and Pens can be only SolidColorBrush. Now also other types of Brushes are supported (LinearGradientBrush, RadialGradientBrush, VisualBrush, etc.).
  • Fixed using opacity on some svg elements.
  • Improved support for RadialGradient when gradientTransform was used.
  • Fixed getting the size of the root svg element when only width or only height is specified (when AutoSize is false).
  • Fixed using Ab2d.ReaderSvg inside WinForms applications.
  • Fixed licensing problems when used on 64-bit OS.

New in version 3.0

  • Added Geometry support:
    new ReadGeometry method with support for advanced geometry optimization and use of resources (Burshes and Pens are defined in resources).
    new ResourceDictionaryWriter class and sample SampleResourceDictionary aplication to simplify creating resources from the svg files
  • Added SvgDrawing control to show svg file as geometries in xaml
  • Added GetXaml method to get the xaml of the read objects (also used in ViewerSvg; it is much more accurate as System.Windows.Markup.XamlWriter.Save)
  • Added GetObjectName method - gets the name of the object. If object does not have a name defined returns null
  • Optimized the output xaml for Path element - now the properties with default values are no longer displayed. This in most cases removes the following properties: StrokeStartLineCap, StrokeEndLineCap, StrokeDashCap, StrokeLineJoin, StrokeMiterLimit, StrokeDashOffset
  • Added support for gradientTransform on radialGradient
  • Added support for known colors in xaml writter - known colors like Black, Yellow can now be written with its name instead of hex display (#FF000000). This feature is used by default but can be disabled with setting UseColorNames in XamlWriterSettings to false.
  • Fixed propagating text styles to child tspan elements (for example if text has underline decoration set, underline will be used also on the child tspan elements - if not overwritten by different text-decoraton)
  • The path is now automatically closed when the last segment ends on the same position as the start segment. Before if the paths were not manually closed, there could be some sharp edges on some parts of the path.
  • Greatly improved performance on svg files where path elements have a lot of segments.
  • Improved setting the size of returned Viewbox.
    If AutoSize is false the returned Viewbox has the size that is defined in the svg file. The previous version did not set the size correctly.
    If the svg width and height are specified in cm, mm or inches the output size in xaml is set in cm.
  • Added AutoSize property to SvgViewbox and SvgDrawing
  • Added BitmapImages property that contains all the bitmaps read with ReaderSvg (also contains images embedded into the svg file)
  • Added ReadPathAsPathGeometry property - If false (default) the path is read as StreamGeometry (better performance). If true the path is read as PathGeometry (worse performance but path can be modified).

New in version 2.3

  • Added AutoSize property. If its value is true (default - as previous version) the size of returned Canvas is calculated from the size of objects it contains. This is useful if you do not care whether the objects were drawn on the Letter or A4 page - you just need the returned object to be as big as its containg objects. If AutoSize is false the size defined in svg element is used for the size of returned Canvas. This is useful if you were working on a Letter page and would like to preserve the positions of objects inside the Letter area.
  • Improved measuring of objects when AutoSize is true (now transformed elements are correctly measured).
  • Improved ellipse - before svg ellipse was converted into Path WPF element - now it is converted into Ellipse WPF element.
  • Added reading elements inside svg link element (before the whole element was skipped). Linking is not supported, but its content is now rendered.
  • Added support for underline and line-through text decorations.
  • Improved processing of text and tspan svg elements.
  • Added help file.

New in version 2.2

  • Added support for FontWeight and Italic FontStyle
  • Added support for Rotate in Tspan (for Text on Path)
  • Fixed fill and stroke brush for Rectangle element
  • Removed RunWithDeveloperLicense Dialog (shown when commercial license is installed, but the application did not have an embedded license.licx file)

New in version 2.1

  • Fixed resolving external references in svgz files
  • Fixed positioning bitmap images
  • Adjusted LinearGradientBrush to show correctly in WPF
  • Added support for MappingMode in LinearGradientBrush
  • Added reading elements inside switch element (before switch was skipped)
  • Improved evaluation mode

New in version 2.0

  • Integrated licensing system - first commercial version.

New in version 1.7

  • Improved reading text - added support for nested tspan elements inside text.
  • Fixed reading text-anchor when defined in parent elements.
  • Improved reading svg files with missing xmlns declarations.
  • Improved performance when reading text. Also now more fonts are rendered correctly.
  • Fixed reading embedded images (sometime the bounds of the TextureBrush) were not read correctly.

New in version 1.6

  • Fixed reading number in exponent format (for example 1.234e-3) in transform element. This fixed the problems when some elements in the read image were placed on the wrong location and with the wrong size.

New in version 1.5

  • Added support for reading svgz (compresses svg) files.
  • Fixed reading some embedded images - sometimes "Invalid character in a Base-64 string." exception was thrown.

New in version 1.4

  • Added SvgViewbox.
  • Fixed closing paths (some paths did not close correctly).

New in version 1.3

  • Fixed reading text for svg files created in Inkscape.
  • Added SvgBounds property (bounds of the elements in svg). Set InnerWidth and InnerHeight as obsolete.
  • Improved read svg elements so they fit correctly into parent object. For example before some svg files produced Viewboxes that exceeded (or were smaller) the size of parent StackPanel. Now the Viewbox should fit into StackPanel.
  • Read FillRule to NonZero or EvenOdd.
  • Fixed some bugs that crashed the ReaderSvg.

New in version 1.2

  • In case text does not have any special stroke or fill set now a TextBlock is used to render text. In previous version all text were converted to paths with made simple texts very complex.
  • Added property UseSimpleText that forces the ReaderSvg to always use TextBlock to render text.
  • Improved reading of svg files.
  • Some small bugfixes.

 

Note: Subscribe to RSS feed or submit your email on downloads section to be informed about new content on the site.

Copyright © 2006-2009 Andrej Benedik s.p. All Rights Reserved.