ViewerSvg and Ab2d.ReaderSvg

Ab2d.ReaderSvg - svg and svgz importer

Use vector graphics from svg files in
WPF and Silverlight applications.
Download now

Ab2d.ReaderSvg - svg and svgz importer

ReaderSvg Usage diagram

Ab2d.ReaderSvg is a class library that can be used to read svg and svgz (compressed svg) files and import its elements into any WPF application.

The Ab2d.ReaderSvg can accurately read shapes defined in svg file, their strokes, fills and other properties. It can read all types of gradients, patterns and markers (line starting and ending shapes). This gives the designers free hands to define their drawings in almost any vector drawing application, save them into svg file and the application developers can than use the drawings in their WPF applications.

The elements in svg file can be converted into shapes or geometry objects. The shapes (Canvas, Ellipse, Path, etc.) are great because they can be simply modified and mouse events can be assigned to them. Geometry objects (GeometryDrawing, PathGeometry, etc.) are a lightweight version of shapes that have much better performance and lower memory usage but are harder to manipulate. Thereforeg graphics objects are ideal for complex drawings. And what is more the read geometry can be furher optimized for even better performance.

For example the following line of XAML is all that is needed to show birthday_cake.svg:
<ab2d:SvgViewbox Source="samples/birthday_cake.svg"/>

Note that Ab2d.ReaderSvg cannot be used in Silverlight applications - but can be used in XBAP Web Browser application.

But it is possible to convert svg files into XAML for Silverlight and Windows Store (Windows RT) applications with the ViewerSvg converter.


Features:
  • Read svg and svgz (compresses svg) files.
  • Preserve objects hierarchy.
  • Get name of the objects from id attribute or from title element.
  • Read embedded bitmap images.
  • Read shapes with their properties.
  • Read linear and radial gradients.
  • Read patterns and convert them into VisualBrush.
  • Read marker elements - line starting and ending shapes (arrows, etc.)
  • Read use and symbol elements (also used for embedded fonts).
  • Read clipPath elements.
  • Read elements in svg file as Shapes or as Geometries (better performance).
  • Optimize read Geometry for better performance.
  • Optionally convert text into Path.
  • Automatically resize the read elements.
  • Get progress report when reading svg file.
  • GetXaml method to get formatted and valid XAML of the read objects.
  • Read custom properties and layers data from svg files created in Microsoft Visio.
  • Included SvgViewbox and SvgDrawing controls that can be used in XAML.
  • Optimize hierarchies of read objects with optimizing the groups or completely flattening the hierarchy.
  • True transform transforms the shape data with changing the actual values instead of using RenderTransform or LayoutTransform.


Samples

Ab2d.ReaderSvg comes with many samples that shows many new possibilities that are opened with the library. There are WPF and Silverlight sample applications. The following screenshots show some of them:

 
 
 


ResourceDictionaryWriter

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


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


Related Blog posts
To see the development history of Ab2d.ReaderSvg and ViewerSvg, check out the related blog posts.


Version history

New in version 5.4

  • Clip path now works on all elements and not just on Canvas.
  • Improved size measurement when AutoSize is set to true and some objects are hidden.
  • Added objects grouping optimizations to ReaderSvg - they can be controlled by new properties in ReaderSvg: OptimizeObjectGroups, FlattenHierarchies, TransformShapes
  • Improved transforming read objects with calling Transform method.
  • Updated SvgBounds after calling Transform method with updateLastReadViewbox set to true.
  • Prevented throwing exception in GetObjectsWithCustomProperties when svg file does not have any custom properties.
  • Added IgnoreRootElementSize property to GeometrySetting - added possibility to remove the transformations or RectangleGeometry based on the size or root element. This can produce nicer XAML.
  • Fixed using transformations on EllipseGeometry and RectGeometry when reading svg file as geometry.
  • Prevented writing "Canvas.Left" and "Canvas.Top" properties in XAML two times.
  • Improved reading geometries when rectangle or ellipse have both Canvas position (Canvas.Left or Canvas.Top) and transformation set.
  • Improved XAML writer to prevent writing Canvas.Left = "0" and Canvas.Top = "0" (zero values can be skipped).

New in version 5.3

  • Fixed reading text with gradient fill or stroke brush.
  • Prevent throwing exception when incorrect number text was used for numbers.
  • Invisible objects (Visibility != Visible) are not exported to geometry any more.
  • Shapes with Opacity set (Opacity < 1.0) are now correctly exported as geometries.

New in version 5.2

  • Added Ab2d.Common.ReaderSvg.TrueTransform class that can be used to transform the read objects.
  • Added Transform method to transform the read svg objects.
  • Added TransformFromPixelsToMM method (can be used to transform svg file saved in pixel units into mm units - for example for Insckape svg files).
  • Added SvgCreator property that specifies the application that was used to create svg file (it it can be determined - currently the supported applications are: Visio, CorelDraw, Adobe Illustrator, Inkscape).

New in version 5.1

  • Fixed writing Image.Stretch into xaml.
  • Fixed reading numbers defined with em unit (for example: "123.45em").
  • Added writing HorizontalAlignment and VerticalAlignment XAML when exporting Image (when using preserveAspectRatio svg element).

New in version 5.0

  • Change build target framework from .Net 3.0 to 3.5 Client Profile.
  • Added support for XAML export for Windows Store applications (Windows Runtime). This can be done with passing an instance of WinRtXamlWriterSettings class to GetXaml method.

New in version 4.9

  • Fixed exporting to XAML when the content of Text property contains curly brackets. Before parser wanted to use them for binding expression.
  • Fixed throwing ArgumentException when right or bottom value in object's bounds is negative.

New in version 4.8

  • Improved reading size of root svg object (improved support for viewBox and added support for preserveAspectRatio).
  • The GetXaml method now preserves the width and height in specified units (cm or inch).
  • Removed memory leak that occurred sometimes when reading embedded images.
  • Improved reading embedded images - they are now read faster and with lower memory usage.
  • Fixed problems when Path bounds were negative and IsSizeSetToPathElement in SilverlightXamlWriterSettings was set to true.
  • Prevented adding PresentationOptions:Freeze="True" text to XAML for Silverlight.
  • Added option to export for Silverlight 5.
  • Fixed reading svg file that contain links to images and were read without specifying the whole path (Read method was called only with svg file name without the full path).

New in version 4.7

  • Added NameFormatString to BaseXamlWriterSettings - it can be used to customize how the object names are written to xaml (custom prefixes and suffixes can be added to names).
  • Added support for preserveAspectRatio property on svg image elements (slice mode with Center and Max are not supported).
  • Improved commercial licensing code to prevent delay caused in RSACryptoServiceProvider under some circumstances.
  • Added Ab2d.ZoomPanel.LicenseHelper.EmbeddedLicenseAssembly property to speed-up looking for embedded license key in commercial version.

New in version 4.6

  • Improved design time support for SvgDrawing control - now the svg file is shown in design time (before the image in design time was shown only in SvgViewbox).
  • Added SvgFileLoading and SvgFileLoaded events to SvgViewbox and SvgDrawing controls.
  • Prevented locking referenced image files.

New in version 4.5

  • Added OverrideMiterLimit property to ReaderSvg. With OverrideMiterLimit it is possible to override the used miter limit value and correct the problems that can be caused by the different rendering of miter limit in svg and WPF.
  • Added SwitchElementProcessingType property to ReaderSvg. It defines how switch children will be processed - possible values: Disabled, ShowFirstDiscardOthers, ShowFirstHideOthers, ShowFirstCollapseOthers or ShowAll.
  • Fixed positioning and sizing of use and embedded svg elements.

New in version 4.4

  • Markers (arrows and other line endings) are now correctly show on line and polyline elements (before they worked only on path elements).
  • markerWidth and markerHeight are correctly handled.
  • Removed obsolete Ab2d.SvgViewbox control - the control has been obsolete for a few versions - Ab2d.Controls.SvgViewbox should be used instead. This also removes the need for Designer assemblies to remove the obsolete control from Designer ToolBox.

New in version 4.3

  • Added ProgressChanged event to ReaderSvg to get report progress when reading file.
  • Improved GetXaml method for Silverlight: Added workaround for displaying Paths in Silverlight in Visual Studio 2010 designer - set Width and Height to Path element.
  • Improved GetXaml method - now the images are written to xaml without need to specify ResolveImagePath delegate. The uri format can be specified with the new DefaultImageUriFormatString property in XamlWritterSettings.
  • Improved reading linked images (not embedded into svg file). Before an exception was thrown when the image file could not be located - now the image object is created but its Source is not set.
  • Fixed reading some svg files with embedded image that reported "Invalid length for a Base-64 char array." error (used custom base64 reader instead of .Net method to read the string).
  • Fixed problems with reading svg files from Microsoft Visio that have more than one layer with the same name defined.
  • Improved ResourceDictionaryWriter class to make it much more customizable. The AddFile and AddStream methods now work correctly. The usedSvgReader, geometrySettings and xamlWritterSettings fields are now protected so they can be changed in derived class. Also the methods CreateReaderSvg, CreateXamlWriterSettings, ReadSvgFile, ReadSvgStream, GetXaml, GetRootResourceKey and ResolveResourceKeyCallback are now protected and virtual so they can be overriden in derived class.
  • Added AddHiddenElements property that specifies if hidden elements are read. If the value of AddHiddenElements is true, than hidden elements are read and have their Visibility property set to Hidden or Collapsed (depends on using display or visibility attribute in svg file).

New in version 4.2

  • Improved performance - time to read some complex svg files is decreased by almost 300%.
  • Added OptimizeStyleProcessing property to ReaderSvg. By default its value is set to true to improved performace. But for some rarely used style declarations the optimized processing can produce wrong results - in those cases set the value to false.
  • Added support for text and tspan baseline-shift.
  • Now polyline and polygon are read as WPF's Polyline and Polygon elements (in previous version they were read as Path).
  • Fixed measuring size - improved for bigger stroke thickness on some shapes (when AutoSize property on ReaderSvg is true - by default).
  • Fixed SvgViewbox and SvgDrawing controls - they do not throw exceptions when Source is not set but some other property like AutoSize is set.

New in version 4.1

  • Improved GetXaml method to enable writing transformations as inner elements instead of attributes (because of visual designer in Visual Studio 2010 and Expression Blend for Silverlight projects) - added UseCanvasPositionForTranslateTransform and WriteTransformationsAsElement properties to BaseXamlWriterSettings class.
  • Added support for some svg files that define url references with "&quot;" - for example: style="stroke: url(&quot;#linearGradient4603&quot;)".
  • Added support for lengthAdjust attribute in text and tspan elements.

New in version 4.0

  • Added support for "marker" svg element - add support for arrows and other line start and end markers
  • Added support for "symbol" svg element - this also adds support for embedded fonts inside svg file
  • Added support for "clipPath" - object clipping
  • Added support for "use" element
  • Added support for xml:space="preserve" attribute - preserves the space before and after the text
  • Added support for textLength attribute in text and tspan elements
  • Fixed reading Rect data written with ',' instead of ' ' (for example '0,0,1,1' works now)
  • The Name property on the read objects is now set (so it is not needed to use NamedObjects dictionary or GetObjectName method). Setting Name can be turned off wuth SetNameProperty no ReaderSvg.
  • Improved the algorithm to correct the names read from svg file (now the name is not changed to lower case any more and also all letters are allowed and not only letters from 'a' to 'z').
  • Added FallbackBrush property to SilverlightXamlWriterSettings so it is possible to control which brush is used instead of VisualBrush that is not supported in Silverlight
  • Added GetLayerNames and GetElementsForLayerName methods to read layer information from svg file defined in Microsoft Visio
  • Added support to read embedded data from svg file created in Microsoft Visio - added the following methods: GetCustomProperties, GetObjectsWithCustomProperties, GetCustomPropertiesDataTable
  • Added NamedObjectsSource property to ReaderSvg to specify the source of the object names - for example for Microsoft Visio or other application that do not allow to set the svg's id attribute, the NamedObjectsSource can be set to NamedObjectsSourceType.Title or NamedObjectsSourceType.TitleIfExist.
  • Added NamedObjectsSource property to SvgViewbox and SvgDrawing

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.

 

Information about changes:
Subscribe to RSS feed to all Blog posts.
Subscribe to RSS feed to Blog posts related to Ab2d.ReaderSvg and ViewerSvg.
Follow me on twitter.
Subscribe to newsletter - enter your email into Newsletter box on my Blog.