Importing SVG files
In addition to making it possible to export VectSharp images as SVG files, VectSharp.SVG also makes it possible to import existing SVG files in order to use them when drawing on the graphics surface.
To do this, you can use the static methods in the VectSharp.SVG.Parser
class. There are three main such methods:
FromString
parses an SVG document contained in astring
. This method takes a singlestring
parameter, corresponding to the SVG source.FromFile
reads the SVG source from the specified file, and then parses it. This method also requires a singlestring
parameter, corresponding to the path to the SVG file.FromStream
instead reads the SVG source from aStream
, which is supplied as a parameter.
Each of these methods returns a Page
, whose Width
and Height
correspond to the size of the viewbox defined in the SVG file, while the page’s Graphics
contains the image.
For example, the following code loads an image from an SVG file, draws it on a new Page
, overlays it with some text, and then saves it again as an SVG file. You can download the SVG file for this example from here.
It is possible for an SVG file to embed other images using the data URI scheme. There is no problem if another SVG image is embedded in this way; however, raster images (such as PNGs or JPEGs) cannot be interpreted directly by VectSharp.
If you need to parse an SVG file that contains raster images embedded using a data URI, you will need to install either the VectSharp.MuPDFUtils package or VectSharp.ImageSharpUtils package. You then need to enable data URI parsing using one of the following lines of code:
These instruct VectSharp to use the specified data URI parser, and only need to be used once in a program, at any time prior to using the methods from the VectSharp.SVG.Parser
class.