Readerwmf, Clipboard and Visio
we use Ab2d.ReaderWmf.dll in 5.4.4299.36701 version, in an WPF Application, by the method GetGeometryFromHemf.
When we copy an object from Visio 2010 (Airport Symbol for example), and paste into this WPF Application, this App crashes with the exception:
"FileFormatException : Unknown file format! Ab2d.ReaderWmf can read only valid metafiles.
Pile d'appel :
at ?.?.?(IntPtr ?)
at Ab2d.ReaderWmf.GetGeometryFromHemf(IntPtr hemf, GeometrySettings settings)
at ... (our code) "
Have you ever seen this problem before ? Is this problem resolved in most recent version of Readerwmf ?
The code used in WPF App is :
ReaderWmf metafileReader = new ReaderWmf();
Metafile metafileImage = null;

IDataObject iData = Clipboard.GetDataObject();
if (iData.GetDataPresent(DataFormats.EnhancedMetafile))
metafileImage = (Metafile)iData.GetData(DataFormats.EnhancedMetafile);

IntPtr hEmf = metafileImage.GetHenhmetafile();

Image myImage = metafileReader.GetGeometryFromHemf(hEmf, GeometrySettings.BasicOptimization);
myImage.Stretch = Stretch.Fill;

string xamlStr = string.Empty;

StringBuilder sb = new StringBuilder();
TextWriter tw = new StringWriter(sb);
XmlTextWriter xw = new XmlTextWriter(tw);
xw.Formatting = Formatting.Indented;

if (myImage != null)
XamlWriter.Save(myImage, xw);
xamlStr = sb.ToString();

Thanks for your responses.
I have tried to copy shapes from Visio 2010 into latest version of Paste2Xaml and everything works here.

You can also try with the latest version - you can install the evaluation version even with installed commercial version (in installer you will need to manually select the ReaderWmf). Because the evaluation version is installed in different folder, this will preserve your commercial version.

But I suspect that the problem is in how you get the hemf of metafile.

Why you do not use the GetGeometryFromClipboard method - it will read the metafile from clipboard for you. It is also used in Paste2Xaml - actually there the GetFromClipboard is used, but this is almost the same just that it reads metafile as Shapes.

This method uses a more low level approach to get hemf (there are some other checks in the code):

isClipboardOpened = Ab2d.Common.User32Wrapper.OpenClipboard(IntPtr.Zero);

if (!isClipboardOpened)
    throw new ExternalException("Cannot open clipboard!");

hemf = Ab2d.Common.User32Wrapper.GetClipboardData(Ab2d.Common.User32Wrapper.CF_ENHMETAFILE);
Andrej Benedik

Forum Jump:

Users browsing this thread:
1 Guest(s)