Silverlight xaml didn't work

I've just downloaded and tested the evaluation version "Ab2d.ReaderWmf.msi".
With Paste2Xaml I could create a valid xaml (export as: Silverlight, Import options: "Get Shapes")

But when I tried the same within source code, I got an invalid Silverlight-xaml export.
Ab2d.ReaderWmf reader = new Ab2d.ReaderWmf();
string xaml = reader.GetXaml(new Ab2d.Common.ReaderWmf.SilverlightXamlWriterSettings());
System.IO.File.WriteAllText(fileName, xaml);

As I figured out, with xaml made by Paste2Xaml have some <Image/>-Tags whereas the code generated xaml contains <DrawingBrush/>-Tags.
I didn't find any options to change this.
Is this an evaluation behaviour?

Hello Peter,

It took me some time before I figured it out.

You are probably using an evaluation version of ReaderWmf.
Here some brushes are replaced by DEMO text - a DrawingBrush is used to draw it.

The demo text can be replaced by gray color by simply setting the ShowDemoTextInEvaluation to false.

Paste2Xaml does this automatically.

So you will need to use:
Ab2d.ReaderWmf reader = new Ab2d.ReaderWmf();
reader.ShowDemoTextInEvaluation = false;
string xaml = reader.GetXaml(new Ab2d.Common.ReaderWmf.SilverlightXamlWriterSettings());
System.IO.File.WriteAllText(fileName, xaml);

I think this should work.
Hi abenedik,

thanks a lot, your solution works fine (your right, currently I'm using the evaluation version).
The resulting xaml do not contain any Image-Tags (like it is when I use Paste2Xam).
Should I add these tags manually, like:
string xaml = reader.GetXaml(...);
then walk through the xml and add these tags...
Or are there better ways for that?

I know how to get the image information by using the EmbeddedBitmapImages property.

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.

// 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;

    _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:

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

    return retImagePath;

thanks a lot - that worked fine.
Now I have to persuade my team leader to buy Ab2d.ReaderWmf
To do these thinks I need Ab2d.ReaderWmf professional ($349.00), right?

Yes, GetXaml method and getting Embedded images are only supported in ReaderWmf Pro version.
Currently i am evaluating ReaderWmf.
I want to display .wmf file into Silverlight 4.0 application.

When i try to add ReaderWmf.dll in to my application it giveing error like this,

You can't add a reference to Ab2d.ReaderWmf.dll as it was not built against the Silverlight runtime. Silverlight projects will only work with Silverlight assemblies.

please help.
ReaderWmf cannot be used in Silverlight - it is only for WPF.

But you can use Paste2Xaml application to create xaml for your Silverlight applications.

To do this start Paste2Xaml, open wmf file or paste come graphics (with metafile format) into the application. Than click export button and select export to Silverlight 4.0. Click save and you will have xaml that from your metafile that can be used in your Silverlight app.

And buying a Paste2Xaml without a ReaderWmf library is much cheaper.
Andrej Benedik

Forum Jump:

Users browsing this thread:
1 Guest(s)