偶看新闻是一部内容开放、自由的互动网络百科全书客观、专业、权威的知识性百科全书
来源:百度文库 编辑:偶看新闻 时间:2024/04/29 18:11:59
The SVG# Reloaded project aims to provide libraries and tools to parse, convert and view the SVG on Windows, especially on the Windows Presentation Foundation platform.
A few months ago, I was looking for a library to convert SVG diagrams to XAML for use in an WPF application. I initially thought some of the available open source projects could easily handle the project, but all failed. The next step was to look for a commercial library, the only one found also failed to handle the project so I have no choice but to create one since I have already told my boss it should not be a problem.
I started with the SharpVectorGraphics (aka SVG#), and hence the name SVG# Reloaded.
This project is currently hosted on Codeplex.
The SharpVectors 1.0 is currently placed in the Beta stage to allow for more time to work on the documentation, fix bugs and accept suggestions. The final release is schedule for January, 2011.
The SVG to XAML/WPF conversion is complete enough and I have used it in the real world application.
The SVG# Reloaded is packaged in several .NET assemblies. The library itself is divided into two parts; the data handling (parsing and modeling) and the rendering. This makes it possible to have different rendering engines and currently we have the WPF based rendering engine and the GDI+ based rendering engine (an improvement over the original SVG# rendering but less complete compared with the WPF rendering engine).
For the WPF package, this is the dependency diagram:
SharpVectors.Core
SharpVectors.Dom
XmlDocument
SharpVectors.Css
SharpVectors.Model
SharpVectors.Runtime
GlyphTypeface.FontUri
SharpVectors.Rendering.Wpf
SharpVectors.Converters
NOTE: The current WPF package will only generate low level objects; Geometry and Drawing. Higher level objects like Shape are not generated, so there is no support for Silverlight.
Geometry
Drawing
Shape
The dependency diagram is shown below:
SharpVectors.Rendering.Gdi
System.Drawing
In this section, we will provide a number of illustrative examples to demonstrate the use of this library.
These sample codes are divided into two parts; converters and controls (including markup extensions). Complete VS.NET solution for these parts are provided for download, and here is a little guide to using these projects/solutions:
SharpVectorsReloaded\Samples\SharpVectorsSamples
SharpVectorsReloaded\Samples\SharpVectorsControlSamples
The SVG to WPF conversion is the main use of this SVG# Reloaded library currently. The other uses will be improved with time.
The following is a diagram showing all the available converters.
FileSvgConverter
DrawingGroup
FileSvgReader
ImageSvgConverter
DirectorySvgConverter
Now, the base class SvgConverter defines the following common properties:
SvgConverter
DrawingSettings
WpfDrawingSettings
All the properties of this class are well documented. The most important properties are:
CultureInfo
FormattedText
IncludeRuntime
true
false
TextAsGeometry
SaveXaml
SaveZaml
UseFrameXamlWriter
We will create a simple console application for illustration, using the following sample SVG file (named, Test.svg):
Sample Text 1
FileSvgConverterSample
For the C# Application:
using System; using SharpVectors.Converters; using SharpVectors.Renderers.Wpf; namespace FileSvgConverterSample { class Program { static void Main(string[] args) { // 1. Create conversion options WpfDrawingSettings settings = new WpfDrawingSettings(); settings.IncludeRuntime = false; settings.TextAsGeometry = true; // 2. Select a file to be converted string svgTestFile = "Test.svg"; // 3. Create a file converter FileSvgConverter converter = new FileSvgConverter(settings); // 4. Perform the conversion to XAML converter.Convert(svgTestFile); } } }
For the VB.NET Application:
Imports SharpVectors.Converters Imports SharpVectors.Renderers.Wpf Module MainModule Sub Main() ' 1. Create conversion options Dim settings As WpfDrawingSettings = New WpfDrawingSettings() settings.IncludeRuntime = False settings.TextAsGeometry = True ' 2. Select a file to be converted Dim svgTestFile As String = "Test.svg" ' 3. Create a file converter Dim converter As FileSvgConverter = New FileSvgConverter(settings) ' 4. Perform the conversion to XAML converter.Convert(svgTestFile) End Sub End Module
These are WPF markup extensions or type converters for handling the SVG files in WPF applications.
Currently, the SVG# Reloaded provides one markup extension, SvgImageExtension, which converts an SVG source file to a DrawingImage.
SvgImageExtension
DrawingImage
SvgImageExtension.Source
For the illustration, we will create a simple WPF Application shown below, each image displayed is an SVG file in the WPF Image control:
Image
SvgImageSample
MainWindow
http://upload.wikimedia.org/wikipedia/commons/c/c7/SVG.svg By Local File By Web File By Local/Resource File By Sub-Folder File By Local/Resource File By Properties
NOTE: As shown above, the local relative path and resource path are similar, and in this case, the local directory is searched at runtime, and if no such file is found, it is assumed to be in the resource.
SvgViewbox control is a WPF Viewbox derived control for viewing the SVG files in WPF applications, and allowing you to use all the Viewbox decorator properties.
SvgViewbox
Viewbox
It wraps a drawing canvas instead of image, so will support interactivity when added to future release of the drawing canvas.
The main property is the SvgViewbox.Source, which is an System.Uri specifying the path to the SVG file.
SvgViewbox.Source
System.Uri
For the illustration, we will create the following WPF sample application:
SvgViewboxSample
Web File Local File 1 Local File 2 Sub-Folder File Resource File
SvgCanvas control is a WPF Canvas derived control for viewing the SVG files in WPF applications, and allowing you to use all the canvas properties.
SvgCanvas
SvgCanvas.Source
SvgCanvasSample
The SVG# Reloaded comes with some applications. The following two may be of interest to you.
This is an application for browsing directory (recursively) of SVG files.
This is an application for viewing the W3C Test Suite compliant results. It has two panes: top and bottom. The top pane is the generated WPF output, the bottom pane is the W3C expected output image.
By the test results, SVG# Reloaded is the most complete SVG reader for WPF!
You can download the strip-down test suite from the project site, since the size is still large for CodeProject file upload limit.
Many parts of WPF are still work in progress. For instance, vertical texts (as in Japanese), which are supported well in GDI+ are still missing. Previously, the team claimed it was due to lack of time, but with WPF 4.0 released without it, one cannot tell their next excuse!
This is the most complete library available to handle the conversion of SVG files to WPF, that I know of. However, SVG itself is a large and complex specification, and even though one will expect the most modern framework, WPF, to easily handle this decade old specification, it is not able.
In this release, the conversion to WPF is complete enough, however, the controls will need some improvements before the final release.
In future version releases, I will work further on the support of the specifications. Any help in this direction is highly welcomed.
This article, along with any associated source code and files, is licensed under The BSD License