04-09-2010, 08:37 AM
Exporting embedded images is more tricy.
You need to provide ResolveImagePath delegate to WpfXamlWriterSettings or SilverlightXamlWriterSettings to resolve the image file names.
Without this images are not exported to xaml.
This is already documented in ReaderSvg help file, but I have forgot to add it also into ReaderWmf help file. It will be there in the next version.
Here is the missing help text and code sample:
The GetXaml method cannot know the image file name and its path - the method does not save the images, it just gets the xaml text. To solve this it is possible to set a ResolveImagePath delegate to the BaseXamlWriterSettings class that can be passed to the GetXaml method. The ResolveImagePath delegate gets the BitmapSource and returns the file name that will be written in the xaml. Without this method the BitmapImage is not created in xaml. The following source code does the trick. It reads the svg file. Than saves all the images to the disk and saves the file names. Than the GetXaml is called with the ResolveImagePath delegate set that resolved the image paths from the dictionary.
You need to provide ResolveImagePath delegate to WpfXamlWriterSettings or SilverlightXamlWriterSettings to resolve the image file names.
Without this images are not exported to xaml.
This is already documented in ReaderSvg help file, but I have forgot to add it also into ReaderWmf help file. It will be there in the next version.
Here is the missing help text and code sample:
The GetXaml method cannot know the image file name and its path - the method does not save the images, it just gets the xaml text. To solve this it is possible to set a ResolveImagePath delegate to the BaseXamlWriterSettings class that can be passed to the GetXaml method. The ResolveImagePath delegate gets the BitmapSource and returns the file name that will be written in the xaml. Without this method the BitmapImage is not created in xaml. The following source code does the trick. It reads the svg file. Than saves all the images to the disk and saves the file names. Than the GetXaml is called with the ResolveImagePath delegate set that resolved the image paths from the dictionary.
Code:
// Use the code with:
// string xamlText = GetXamlWithImages("images_test.emf", @"c:\temp\", "image_{0}.png");
private Dictionary<BitmapSource, string> _imageFileNames;
private string GetXamlWithImages(string metaFileName, string imagesPath, string imageFormatString)
{
string xamlText;
Ab2d.ReaderWmf myReaderWmf;
Ab2d.Common.ReaderWmf.WpfXamlWriterSettings xamlSettings;
// Read the svg file
myReaderWmf = new Ab2d.ReaderWmf();
myReaderWmf.ShowDemoTextInEvaluation = false;
myReaderWmf.Read(metaFileName);
_imageFileNames = new Dictionary<BitmapSource, string>();
for (int i = 0; i < myReaderWmf.EmbeddedBitmapImages.Count; i++)
{
BitmapSource oneBitmap;
string filePath;
oneBitmap = myReaderWmf.EmbeddedBitmapImages[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.ReaderWmf.WpfXamlWriterSettings();
xamlSettings.ResolveImagePath = ResolveImagePath;
xamlText = myReaderWmf.GetXaml(xamlSettings);
return xamlText;
}
private string ResolveImagePath(BitmapSource imageToResolve)
{
string retImagePath;
if (imageToResolve == null)
retImagePath = "";
else
_imageFileNames.TryGetValue(imageToResolve, out retImagePath);
return retImagePath;
}
