ReaderSvgGetXaml Method (BaseXamlWriterSettings) |
Namespace: Ab2d
public string GetXaml( BaseXamlWriterSettings settings )
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.
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; }