Click or drag to resize
AB4D logo

ReaderSvgGetXaml Method

Gets xaml of the last read svg or svgz file.
Overload List
  NameDescription
Public methodCode exampleGetXaml
Gets xaml of the last read svg or svgz file with the default setting for WPF.
Public methodCode exampleGetXaml(BaseXamlWriterSettings)
Gets xaml of the last read svg or svgz.
Top
Remarks

Read or ReadGeometry must be called before using GetXaml.

To specify detailed options for xaml or to get xaml for Silverlight use GetXaml method.

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

sampleViewbox = myReader.Read(@"c:\mySample.svg");
xaml = myReader.GetXaml();

System.IO.File.WriteAllText(@"c:\mySample.xaml", xaml);
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();
    myReaderSvg.Read(svgFileName);


    _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: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/9f23dde5-f281-4175-a6a2-5f4ad14a4dfe/?lc=1033&ffpr=0
           enc.Frames.Add(BitmapFrame.Create(oneBitmap));
            enc.Save(fs);

            _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 = "";
    else
        _imageFileNames.TryGetValue(imageToResolve, out retImagePath);

    return retImagePath;
}
See Also