|
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;assembly=Ab2d.ReaderSvg"
- Use SvgViewbox to read the svg file - set Source property to svg file location.
Source property can be uses just as Source of the Image control. Depending on the location and svg file build action use of 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"/>
Read svg file in code:
It is possible to create an instance of SvgViewbox in code:
Ab2d.SvgViewbox svgButton = new Ab2d.SvgViewbox();
svgButton.Source = new Uri("loadButton.svg");
myButtonsStackPanel.Children.Add(svgButton);
It is also possible to use ReaderSvg directly:
Viewbox svgButton = Ab2d.ReaderSvg.Instance.Read("c:\\temp\\openButton.svg");
myButtonsStackPanel.Children.Add(svgButton);
Note that the when using Read method with the file name it must be a real file with full path and not a resource of Uri as with the SvgViewbox's Source property. With Read method it is also possible to read svg file from stream.
Note: From version 1.5 on it is also possible to read svgz (compressed svg) files.
Public Properties:
Before reading svg file you can set the Width and Height properties of ReaderSvg
to specify the size of returned Viewbox - if size is not set the size of UIElement
that will host the read Viewbox will be used.
After the svg file was read you can access the following properties:
NamedObjects is a Dictionary<string, FrameworkElement>
that can be used to get the element by its id as defined in the svg file. This can
be a single Path or it can be a Canvas that contains other elements - like Group.
Title and Description
values can be read if they are defined in svg file.
InnerWidth and InnerHeight
contain the size of all elements in svg file.
To get or set properties of ReaderSvg it is recommended to create an instance of
ReaderSvg and not to use its static Instance property. For example:
XAML: <ab2d:SvgViewbox Name="UserButtonsViewbox" Source="UserButtons.svg"/>
code: Canvas playButton = UserButtonsViewbox.NamedObjects["PlayButton"] as Canvas;
or if ReaderSvg is used directly:
Ab2d.ReaderSvg myReaderSvg = new Ab2d.ReaderSvg();
Viewbox svgScene = myReaderSvg.Read(fileName);
Canvas playButton = myReaderSvg.NamedObjects["PlayButton"] as Canvas;
ReaderSvg Sample application:
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 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.
 DOWNLOAD All Ab2d tools, libraries and samples (1.13 MB) [1 Apr 2008]  (Paste2Xaml, ViewerSvg, Ab2d.ReaderWmf, Ab2d.ReaderSvg, ReaderSvg sample app.)
System requirements: Windows XP, Windows Vista (recommended) .Net Framework 3.0 (also works with 3.5) Visual Studio 2005 (can be imported into VS 2008) - for samples
Note: Subscribe to RSS feed or submit your email on downloads
section to be informed about new content on the site.
|