ReaderSvg Class |
Namespace: Ab2d
public class ReaderSvg
The ReaderSvg type exposes the following members.
Name | Description | |
---|---|---|
ReaderSvg |
Constructor of ReaderSvg
ReaderSvg is a class library for reading svg files and converting its elements into WPF elements
|
Name | Description | |
---|---|---|
AddHiddenElements |
Gets or sets a Boolean that specifies if hidden elements are read. Default value is true.
| |
AddNewCanvasForTransformations |
If true (default = false) transformations on elements are not set on the element itself but are set to a new canvas and the element is added as its parent.
This property is added for backward compatibility with versions 2.3 and older.
| |
AutoSize |
When AutoSize is false the size of read svg objects is determined by the width and height defined in the svg header.
For example when svg header define the size as Letter, than the read svg objects will retain the position within the letter area.
When AutoSize is set to true (by default), the size defined in svg header is ignored. Instead the size of the actual svg content is calculated
so that the read objects do not have any empty area around them. The calculated or read size of svg elements can be read from SvgBounds property.
| |
BitmapImages |
Gets list of all bitmap images that are defined in svg file and cannot be converted to vector elements.
| |
ConvertTransformationsToMatrixTransform |
Gets or sets a Boolean that specifies if all transformations are converted to MatrixTransform.
Default values is false that preserves the type of transformation defined in svg file.
| |
Description |
Description defined in svg file
| |
FlattenHierarchies |
Gets or sets a Boolean that specifies if all child groups (Canvases or DrawingGroup) are removed and all elements are placed under the root group.
The group transformations are added to child objects so that the rendered drawing looks the same.
The exception to the removed groups are the groups that define clipping.
| |
HasCustomProperties |
Gets a Boolean that specifies if the read svg files has some custom properties defined.
| |
Height |
Gets or sets the Height of the returned Viewbox element
| |
HeightInUnits |
Gets a string that represents height of svg element in specified units (for example "2cm" or "2in"). If the read sgv element does not specify units, than this value is null.
| |
InnerCanvas |
Gets the inner canvas without the Viewbox control. See also SvgBounds.
| |
InnerHeight | Obsolete.
Gets Height of all read svg elements
| |
InnerWidth | Obsolete.
Gets the Width of all read svg elements
| |
Instance |
Gets static instance of Ab2d.ReaderSvg so you do not need to create your own instance of it.
| |
IsCorrectingNamesForWpf |
Gets or sets a Boolean that specifies if the names are corrected so that they can be used in XAML.
The following rule is applied in this case: name must start with a letter or underscore and can contain only letters, digits, or underscores.
If true, then invalid characters are replaced with underscore. If false, then the NamedObjects dictionary contains unchanged names, but the GetXaml method still corrects the names.
Default value is true (for backwards compatibility).
| |
LastReadImage |
Gets the lastly read svg as WPF's Image element (used when ReadGeometry is used, when using Read method see LastReadViewbox)
| |
LastReadViewbox |
Gets the lastly read svg as WPF's Viewbox element (used when Read is used, when using ReadGeometry method see LastReadImage)
| |
LicenseInfo |
License information
| |
MakeNamesUnique |
Gets or sets a Boolean that specifies if ReaderSvg adds index text to the names of objects that were already used to make them unique.
For example if svg object has a name "SvgObject" and if the second svg object has the same name, the second svg object will be named "SvgObject2".
Default value is true.
| |
NamedObjects |
Dictionary with elements ids as keys and its appropriate objects as values
Ids (keys) can be defined in a drawing application
| |
NamedObjectsSource |
Gets or sets one of the ReaderSvgNamedObjectsSourceType as the source to get the object's name. Default value is NamedObjectsSourceType.Auto.
| |
OptimizeObjectGroups |
Gets or sets a Boolean that specifies if groups (Canvas or DrawingGroup) are optimized in such a way that they groups that contain only one other group (Canvas or DrawingGroup) are removed.
Default value is false which preserves the groups as they are defined in the original svg file.
| |
OptimizeStyleProcessing |
Gets or sets a Boolean that specifies if ReaderSvg is using an optimized style processing that improves speed of reading svg file (default value is true).
| |
OverrideMiterLimit |
Gets or sets a value that is used instead of the miter limit specified in the svg file.
If value is 0, than the miter limit from svg file is used. Default value is 0.
| |
ReadForeignObjects |
Gets or sets a boolean that specifies if foreignObject elements in svg files are read (currently only Visio metafiles are supported). Default value is true.
| |
ReadPathAsPathGeometry |
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).
| |
SetNameProperty |
Gets or sets a Boolean that determines if the object's Name property is set to the name defined in svg file (default value is true).
| |
SetSvgNameToParentTransformCanvas |
Gets or sets a boolean that specifies if object name is set to the Canvas that is created to contain object's transformation when the object also defines the Canvas.Left and Canvas.Top properties. Default value is false.
| |
ShowDemoTextInEvaluation | Obsolete.
ShowDemoTextInEvaluation is not used any more.
| |
SvgBounds |
Gets the bounds of the svg elements.
Note: Usually svg elements does not start at (0,0) but are places in a virtual letter. To work only with svg elements so they can be sized according to the host element (for example StackPanel, Page, etc.) two Canvases and a Viewbox is used. But with SvgBounds property it is possible to get the original bounds of the svg elements. If AutoSize is set to false, the SvgBounds are get from the root svg element.
Also this property makes InnerWeight and InnerHeight obsolete.
| |
SvgCreator |
Gets an application that was used to create this svg file.
| |
SwitchElementProcessingType |
Specifies which children of the svg switch element will be imported and shown. Default value is ShowFirstCollapseOthers (imports all switch children but shows only the first one; others are Collapsed).
| |
Title |
Title defined in svg file
| |
TransformCanvasNameFormatString |
Gets or sets a string that specifies how the Transform Canvas that is created to contain object's transformation is named.
Default value is "{0}_transform".
| |
TransformShapes |
Gets or sets a Boolean that specifies if all shape objects (Rectangles, Ellipses, Path, Polygons, etc.) that have simple transformation (only translate or scale) or canvas position are transformed with changing their data
with the specified transformation. This can remove RenderTransform, Canvas.Left and Canvas.Top from most of the shapes (exception are shapes that render transform).
| |
UseOldSizeMeasurement |
If true than size measurement from ReaderSvg v1.2 is used - if your application depends on previous sizes of read elements than use this property.
Note: Size measurement is used so svg elements can be used inside other controls - so its size is adjusted regarding to the desired size - set with Width and Height properties or by the parent element.
This property will exit only in ReaderSvg v1.3
| |
UseOnlyInstalledFontFamilies |
When UseOnlyInstalledFontFamilies is false (by default), then FontFamily is created also with font names that are not installed on the system (this preserves the original font family name when exported to XAML).
When UseOnlyInstalledFontFamilies is true and font family name in svg file is not installed on the system, then Arial font family is used.
| |
UseSimpleText |
If true text is always rendered as TextBlock - also if text is using custom strokes and fill patters.
If false than in case of custom strokes and fill patterns text is converted into Path elements and the appropriate effects are applied to it.
Default value is false;
| |
Width |
Gets or sets the Width of the returned Viewbox element
| |
WidthInUnits |
Gets a string that represents width of svg element in specified units (for example "2cm" or "2in"). If the read sgv element does not specify units, than this value is null.
|
Name | Description | |
---|---|---|
GetCustomProperties(Object) |
Returns a Dictionary of custom properties defined for the object with objectName (only custom properties defined in Microsoft Visio are supported).
| |
GetCustomProperties(String) |
Returns a Dictionary of custom properties defined for the object with objectName (only custom properties defined in Microsoft Visio are supported).
| |
GetCustomPropertiesDataTable |
Returns a System.Data.DataTable with all custom properties defined in svg file (only custom properties defined in Microsoft Visio are supported).
| |
GetElementsForLayerName |
Returns a List of UIElements that are assigned to the layer with layerName (layers can be only defined in Microsoft Visio).
| |
GetElementSvgText |
GetElementSvgText can be called after the svg file has been read.
It returns svg text of the element with with the specified elementName and elementNameType.
If the element is not found, null is returned.
| |
GetLayerNames |
Returns a List of layer names that were defined in Microsoft Visio.
| |
GetObjectName |
Gets the name of the object read with ReadSvg. This is the opposite as using NamedObjects dictionary. Returns null if object is not found or it does not have a name defined.
| |
GetObjectsWithCustomProperties |
Returns List of objects that have custom properties defined (only custom properties defined in Microsoft Visio are supported).
| |
GetXaml |
Gets xaml of the last read svg or svgz file with the default setting for WPF.
| |
GetXaml(BaseXamlWriterSettings) |
Gets xaml of the last read svg or svgz.
| |
Read(Stream) |
Reads svg file from stream - useful for reading svg files from resources. The file is read as Shapes (Canvas, Path, Ellipse, etc.).
| |
Read(String) |
Reads svg or svgz file from file system or from online source. The file is read as Shapes (Canvas, Path, Ellipse, etc.).
| |
ReadGeometry(Stream) |
Reads svg or svgz file and returns System.Windows.Controls.Image with elements as Drawings (DrawingGeometry, EllipseGeometry, GlyphRunDrawing etc.) with no optimization.
| |
ReadGeometry(String) |
Reads svg or svgz file and returns System.Windows.Controls.Image with elements as Drawings (DrawingGeometry, EllipseGeometry, GlyphRunDrawing etc.) with no optimization.
| |
ReadGeometry(Stream, GeometrySettings) |
Reads svg or svgz file and returns System.Windows.Controls.Image with elements as Drawings (DrawingGeometry, EllipseGeometry, GlyphRunDrawing etc.).
| |
ReadGeometry(String, GeometrySettings) |
Reads svg or svgz file and returns System.Windows.Controls.Image with elements as Drawings (DrawingGeometry, EllipseGeometry, GlyphRunDrawing etc.).
| |
RenderToBitmap(Brush) |
Renders the read svg objects into bitmap. Bitmap will have the same size as read svg object and will be rendered at 96 dpi.
| |
RenderToBitmap(Brush, Int32) |
Renders the read svg objects into bitmap. Bitmap will have the same size as read svg object and will be rendered at dpi specified in the parameter.
| |
RenderToBitmap(Int32, Int32, Brush) |
Renders the read svg objects into bitmap with custom width and height and at 96 dpi.
| |
RenderToBitmap(Int32, Int32, Int32, Brush) |
Renders the read svg objects into bitmap with custom width and height and at dpi specified with parameter to the method.
| |
Transform |
Transform method transforms all the coordinates, sizes and other values in the last read Viewbox with using transformation.
| |
TransformFromPixelsToMM |
TransformFromPixelsToMM method transforms all the coordinates, sizes and other values in the last read Viewbox in such a way that it converts all units in pixels into millimeters with using specified dpi setting.
|
Name | Description | |
---|---|---|
ProgressChanged |
ProgressChanged event can be used to get notifications of how much the svg file has been read.
|
Name | Description | |
---|---|---|
AfterWpfObjectCreatedCallback |
AfterWpfObjectCreatedCallback can be used to provide a way to customize WPF objects that are created with ReaderSvg from svg elements. See remarks for more info.
| |
BeforeWpfObjectCreatedCallback |
BeforeWpfObjectCreatedCallback can be used to provide a way to create custom WPF objects from svg elements. See remarks for more info.
| |
GetCustomSvgElementNameCallback |
GetCustomSvgElementNameCallback can be used to provide custom naming of WPF objects created from svg elements.
The object names are written into NamedObjects dictionary.
Note that when IsCorrectingNamesForWpf or MakeNamesUnique are true, the name can be corrected after calling the GetCustomSvgElementNameCallback.
Also names used for XAML are always corrected.
|
Ab2d.ReaderSvg class can be used to read svg and svgz files from the code.
They can be read as Shapes with Read(String) method or as Drawing objects with ReadGeometry(String).
After the file has been read, it is possible to get the xaml of the read Viewbox or Image with GetXaml method.
If your application needs images and shapes from many svg and svgz files, it is recommended that they are stored in a ResourceDictionary. To simplify creating ResourceDictionaries a ResourceDictionaryWriter class can be used. There is also a sample Ab2d.ReaderSvg.ResourceDictionaryWriter application available to simplify this process.
When svg file was created with Microsoft Visio, than the ReaderSvg can also read custom properties and layers data from the svg file. To read more about reading custom properties see GetCustomProperties(String) method. To read more about accessing layers data see GetLayerNames method.
The following example shows the most simple use of ReaderSvg. It loads the mySample.svg file and adds it to the myStackPanel.
Viewbox sampleViewbox = Ab2d.ReaderSvg.Instance.Read(@"c:\mySample.svg");
myStackPanel.Children.Add(sampleViewbox);
The following sample shows how to read svg from Application's Resource. The ReadGeometry(String, GeometrySettings) reads svg file into Drawing objects and fully optimizes it for best performance.
Image clipartFromResource; Ab2d.Common.ReaderSvg.GeometrySettings settings; // gets Drawing objects optimized for best performance // NOTE: Because of heavy optimization it is possible that some anomalies appear on the image settings = Ab2d.Common.ReaderSvg.GeometrySettings.FullOptimization; Uri uri = new Uri("/cliparts/mySample.svg", UriKind.Relative); using (Stream svgStream = Application.GetResourceStream(uri).Stream) { clipartFromResource = Ab2d.ReaderSvg.Instance.ReadGeometry(svgStream, settings); } myStackPanel.Children.Add(clipartFromResource);
The following sample reads the mySample.svg file, gets its xaml for Silverlight and saves it into a file.
var myReader = new Ab2d.ReaderSvg(); Viewbox sampleViewbox = myReader.Read(@"c:\mySample.svg"); string xaml = myReader.GetXaml(new Ab2d.Common.ReaderSvg.SilverlightXamlWriterSettings()); System.IO.File.WriteAllText(@"c:\mySample_for_Silverlight.xaml", xaml);
The next example uses the RenderToBitmap(Int32, Int32, Brush) method to render the read svg file to image with custom size and saves it to disk:
var readerSvg = new Ab2d.ReaderSvg(); readerSvg.Read(@"c:\mySample.svg"); // Render to bitmap with 1024 width - height will be set automatically so that the // Image aspect ratio is preserved var wpfBitmap = readerSvg.RenderToBitmap(1024, 0, Brushes.White); var encoder = new System.Windows.Media.Imaging.PngBitmapEncoder(); // Uncomment to save to jpg //var encoder = new JpegBitmapEncoder(); //encoder.QualityLevel = 85; encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(wpfBitmap)); using (var stream = System.IO.File.OpenWrite("c:\\svgImage.png")) { encoder.Save(stream); }