Click or drag to resize
AB4D logo

SvgDrawing Class

SvgDrawing is a Viewbox control that shows elements defined in svg file as WPF geometry drawing objects. 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 SvgDrawing : FrameworkElement, IUriContext, 
	ISupportInitialize

The SvgDrawing type exposes the following members.

Constructors
  NameDescription
Public methodSvgDrawing
Constructor
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 propertyInnerImage
Gets the Image returned by last read with ReaderSvg.
Public propertyInnerReaderSvg
Gets the ReaderSvg that was used to read the svg or svgz file.
Public propertyNamedObjectsSource
Gets or sets one of the ReaderSvgNamedObjectsSourceType as the source to get the object's name. Default value is NamedObjectsSourceType.Auto.
Public propertyOptimizationPercent
Gets or sets the geometry optimization percent. 0 means no optimization, 100 means full optimization.
Public propertySource
Gets or sets the Source of the svg or svgz file
Public propertySourceStream
Stream that is used to read svg file. When this property is set (not null) it is used instead of Source property.
Public propertyStretch
Gets or sets the SvgDrawing.Windows.Media.Stretch mode, which determines how content fits into the available space.
Public propertyStretchDirection
Gets or sets the System.Windows.Controls.StretchDirection, which determines how scaling is applied to the contents of a SvgDrawing.
Top
Methods
  NameDescription
Protected methodArrangeOverride
When overridden in a derived class, positions child elements and determines a size for a FrameworkElement derived class.
(Overrides FrameworkElementArrangeOverride(Size).)
Protected methodMeasureOverride
When overridden in a derived class, measures the size in layout required for child elements and determines a size for the FrameworkElement-derived class.
(Overrides FrameworkElementMeasureOverride(Size).)
Protected methodOnCreateAutomationPeer
OnCreateAutomationPeer
(Overrides UIElementOnCreateAutomationPeer.)
Protected methodOnRender
OnRender
(Overrides UIElementOnRender(DrawingContext).)
Protected methodOnSvgFileLoaded
OnSvgFileLoaded is called after the svg file has been read. The method can be overridden 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, Int32) Obsolete.
Reads svg or svgz file from sourceUri and with optimizationPercent
Protected methodReadSvg(Uri, Stream, Int32)
Reads svg file from sourceUri or sourceStream and with the specified optimizationPercent
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 memberOptimizationPercentProperty
OptimizationPercentProperty
Public fieldStatic memberSourceProperty
SourceProperty
Public fieldStatic memberSourceStreamProperty
SourceProperty
Public fieldStatic memberStretchDirectionProperty
StretchDirectionProperty
Public fieldStatic memberStretchProperty
StretchProperty
Top
Remarks

The difference between SvgDrawing and SvgViewbox is that SvgViewbox gets objects as Shapes (Canvas, Path, Polyline, etc) and SvgDrawing produces Drawings (DrawingGeometry, StreamGeometry, etc). Drawings have better performance and lower memory consumption, but Shapes support more functionality - layout, focus, mouse events, etc.

For more information about reading geometers see ReadGeometry(String, GeometrySettings).

For more information about setting the optimization percent see FromOptimizationPercentage(Int32).

SvgViewbox can be used in xaml. If you would like to read svg file in code and need some more advanced features and options please use ReaderSvg

To read the svg file set Source 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 myClipart.svg inside a Grid (the svg file's BuildAction is set to Resource). The svg file is read as Drawing that is fully optimized for better performance (if animated).
<Window x:Class="Ab2d.ReaderSvgSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ab2d="clr-namespace:Ab2d.Controls;assembly=Ab2d.ReaderSvg"
        Title="Window1" Height="500" Width="500"/>
     <Grid>
         <ab2d:SvgDrawing Name="mySvgDrawing" Source="myClipart.svg"
                          OptimizationPercent="100" Stretch="Uniform"/>  
     </Grid>
</Window>

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.SvgDrawing();
    mySvgImage.SourceStream = streamResourceInfo.Stream;
    mySvgImage.Width = 200;
    mySvgImage.Height = 100;

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