Click or drag to resize
AB4D logo

SvgViewbox Class

SvgViewbox is a Viewbox control that shows elements defined in svg file as WPF Shapes in a Viewbox control. The source of svg file is set by Source or SourceStream properties.
Inheritance Hierarchy

Namespace:  Ab2d.Controls
Assembly:  Ab2d.ReaderSvg (in Ab2d.ReaderSvg.dll) Version: 7.1.7295.1040
Syntax
C#
public class SvgViewbox : Viewbox, IUriContext, 
	ISupportInitialize

The SvgViewbox type exposes the following members.

Constructors
  NameDescription
Public methodSvgViewbox
Constructor for SvgViewbox.
Top
Properties
  NameDescription
Public propertyAutoSize
If true (default) the size of main canvas is calculated from the size of all inner objects. This means that the returned objects will be just as big as its contain objects. If false the size of svg element is used for the size of main canvas. This is useful for example if you were working on a Letter area and would like to preserve the position of objects inside the Letter.
Public propertyInnerReaderSvg
Gets the ReaderSvg that was used to read the svg file. Useful to get the properties of ReaderSvg for example NamedObjects (gets the names of objects as defined in svg file)
Public propertyNamedObjects
Dictionary defined from elements in svg file. Elements ids are keys and its appropriate objects are values.
Public propertyNamedObjectsSource
Gets or sets one of the ReaderSvgNamedObjectsSourceType as the source to get the object's name. Default value is NamedObjectsSourceType.Auto.
Public propertySource
Source or the svg or svgz image.
Public propertySourceStream
Stream that is used to read svg file. When this property is set (not null) it is used instead of Source property.
Top
Methods
  NameDescription
Protected methodOnCreateAutomationPeer
Returns class-specific AutomationPeer implementations for the Windows Presentation Foundation (WPF) infrastructure.
(Overrides UIElementOnCreateAutomationPeer.)
Protected methodOnSvgFileLoaded
OnSvgFileLoaded is called after the svg file has been read. The method can be overriden in derived class.
Protected methodOnSvgFileLoading
OnSvgFileLoading is called before the svg file is read. The method can be overridden in derived class.
Protected methodReadSvg(Uri) Obsolete.
Reads svg file from sourceUri. This method can be overridden to provide reading svg files from some other source.
Protected methodReadSvg(Uri, Stream)
Reads svg file from sourceUri or sourceStream. This method can be overridden to provide reading svg files from some other source.
Top
Events
  NameDescription
Public eventSvgFileLoaded
Occurs when the reading of svg file is completed.
Public eventSvgFileLoading
Occurs before the reading of svg file is started.
Top
Fields
  NameDescription
Public fieldStatic memberAutoSizeProperty
AutoSizeProperty
Public fieldStatic memberSourceProperty
SourceProperty
Public fieldStatic memberSourceStreamProperty
SourceProperty
Top
Remarks

SvgViewbox is using ReaderSvg library to read the svg file and convert it into WPF elements.

To read the svg file set Source or SourceStream property. It supports the Pack URI (see also "http://msdn2.microsoft.com/en-us/library/aa970069.aspx#Site_of_Origin_File_Pack_URIs"). Here are some of the example Sources that can be used to read svg file from various locations:

URI stringDescription
Subfolder/ResourceFile.svgReads ResourceFile.svg from the application resources (embeded in the assembly) under Subfolder - set the file's Build Action to Resource
pack://application:,,,/Subfolder/ResourceFile.svgSame as previous but this time with full pack URI
ContentFile.svgReads ContentFile.svg from the same folder as the application is running - set the file's Build Action to Content
pack://application:,,,/ReferencedAssembly;component/ResourceFile.svgReads ResourceFile.svg from the ReferencedAssembly resources.
pack://siteoforigin:,,,/File.svgReads File.svg from the same folder as the application is running - this is a loose file that means it is not included in the project
c:\images\File.svgReads File.svg from the specified file on the disk
http://www.mysite.com/images/File.svgReads File.svg from specified url location

When using in code create new Uri with on of the above strings and set it to Source property.

Examples

The following example shows how to use SvgViewbox xaml.

XAML
<ab2d:SvgViewbox Source="Images/MyImage.svg" Width="200" Height="100"/>

The following example shows how to use SvgViewbox in code:

C#
var mySvgImage = new Ab2d.Controls.SvgViewbox();
mySvgImage.Source = new Uri("Images/MyImage.svg");
mySvgImage.Width = 200;
mySvgImage.Height = 100;

myStackPanel.Controls.Add(mySvgImage);

The following example shows how to read svg file from stream:

C#
var streamResourceInfo = Application.GetResourceStream(new Uri("pack://application:,,,/Resources/home1.svg"));
if (streamResourceInfo != null)
{
    var mySvgImage = new Ab2d.Controls.SvgViewbox();
    mySvgImage.SourceStream = streamResourceInfo.Stream;
    mySvgImage.Width = 200;
    mySvgImage.Height = 100;

    myStackPanel.Controls.Add(mySvgImage);
}
See Also