Click or drag to resize
AB4D logo

ReaderSvgGetXaml Method (BaseXamlWriterSettings)

Gets xaml of the last read svg or svgz.

Namespace:  Ab2d
Assembly:  Ab2d.ReaderSvg (in Ab2d.ReaderSvg.dll) Version: 7.1.7295.1040
public string GetXaml(
	BaseXamlWriterSettings settings


Type: Ab2d.Common.ReaderSvgBaseXamlWriterSettings
XamlWriterSettings that define advanced options for xaml writer. To get xaml for WPF, use Ab2d.Common.ReaderSvg.WpfXamlWriterSettings. For Silverlight use Ab2d.Common.ReaderSvg.SilverlightXamlWriterSettings.

Return Value

Type: String
xaml of the last read svg or svgz

Read or ReadGeometry must be called before using GetXaml.

To get xaml for WPF, use Ab2d.Common.ReaderSvg.WpfXamlWriterSettings. For Silverlight use Ab2d.Common.ReaderSvg.SilverlightXamlWriterSettings.

If the read svg file contains bitmap images, it is possible to use the ResolveImagePathDelegate delegate to specify the path to the image that will be used in the returned xaml. If the delegate is not specified, the format of the image uri-s is defined by DefaultImageUriFormatString. With its default value the image uri-s are written as "image_0.png", "image_1.png", and so on. The sample below described the use of ResolveImagePathDelegate delegate.

The following sample reads the mySample.svg file, gets its xaml for Silverlight and saves it into a file.
string xaml;
Viewbox sampleViewbox;

sampleViewbox = myReader.Read(@"c:\mySample.svg");
xaml = myReader.GetXaml(new Ab2d.Common.ReaderSvg.SilverlightXamlWriterSettings());

System.IO.File.WriteAllText(@"c:\mySample_for_Silverlight.xaml", xaml);

The GetXaml method by default writes image uri-s as "image_0.png", "image_1.png", and so on. The default format for the uri is controlled by DefaultImageUriFormatString. With ResolveImagePathDelegate it is possible to control the image uri of each of the used images. The following code reads the svg file, saves the images to disk and creates xaml text with image uri-s that point to saved images.

// Use the code with:
// string xamlText = GetXaml("images_test.svg", @"c:\temp\", "image_{0}.png");

private Dictionary<BitmapSource, string> _imageFileNames;

private string GetXaml(string svgFileName, string imagesPath, string imageFormatString)
    string xamlText;
    Ab2d.ReaderSvg myReaderSvg;
    Ab2d.Common.ReaderSvg.WpfXamlWriterSettings xamlSettings;

    // Read the svg file
    myReaderSvg = new Ab2d.ReaderSvg();

    _imageFileNames = new Dictionary<BitmapSource, string>();

    for (int i = 0; i < myReaderSvg.BitmapImages.Count; i++)
        BitmapSource oneBitmap;
        string filePath;

        oneBitmap = myReaderSvg.BitmapImages[i];
        filePath = System.IO.Path.Combine(imagesPath, string.Format(imageFormatString, i + 1));

        using (System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
            PngBitmapEncoder enc = new PngBitmapEncoder();

           // NOTE:
           // If break on exception is turned on in VS, 
           // the next line will throw an exception, but it is handled in .net framework (so click continue)
           // See also:

            _imageFileNames.Add(oneBitmap, filePath);

    xamlSettings = new Ab2d.Common.ReaderSvg.WpfXamlWriterSettings();
    xamlSettings.ResolveImagePath = ResolveImagePath;

    xamlText = myReaderSvg.GetXaml(xamlSettings);

    return xamlText;

private string ResolveImagePath(BitmapSource imageToResolve)
    string retImagePath;

    if (imageToResolve == null)
        retImagePath = "";
        _imageFileNames.TryGetValue(imageToResolve, out retImagePath);

    return retImagePath;
See Also