Copy MetaFile from the clipboard???

B

bern11

I can get bitmaps from the clipboard, but how do I get Metafiles?
The specific instance I am testing is copying a piece of Word clip-art
into the clipboard and trying to read it in an application. It appears
as a metafile type but isn't recognized as an image.

Given:

DataObject copyObject;
copyObject = (DataObject)Clipboard.GetDataObject();
string[] types = copyObject.GetFormats();

and types contains "MetaFilePict", this fails:
Metafile tmp = (Metafile) copyObject.GetData("MetaFilePict");

with:
'Unable to cast object of type 'System.IO.MemoryStream' to type
'System.Drawing.Imaging.Metafile'.

Also, copyObject.ContainsImage() is false.

How do I get the metafile out?
 
P

Peter Duniho

[...]
and types contains "MetaFilePict", this fails:
Metafile tmp = (Metafile) copyObject.GetData("MetaFilePict");

with:
'Unable to cast object of type 'System.IO.MemoryStream' to type
'System.Drawing.Imaging.Metafile'.

Also, copyObject.ContainsImage() is false.

How do I get the metafile out?

Well, you've got an object of type MemoryStream. So one thing you could
try is passing that to Image.FromStream() and see what happens.

However, my experience suggests that that won't work either. You should
definitely try it, just in case something's changed since last year. But
otherwise, you may find this thread helpful:
http://groups.google.com/group/micr...read/thread/50cfd03caa3f31b9/2b9c42a12b6fb7e2

The short story: the Windows clipboard, metafiles, and .NET don't really
get along very well. You are likely to need to use p/invoke (interop) to
get this to work (and only with the "Enhanced Metafile" format, not the
older "Metafile" format).

Pete
 
B

bern11

Peter said:
[...]
and types contains "MetaFilePict", this fails:
Metafile tmp = (Metafile) copyObject.GetData("MetaFilePict");

with:
'Unable to cast object of type 'System.IO.MemoryStream' to type
'System.Drawing.Imaging.Metafile'.

Also, copyObject.ContainsImage() is false.

How do I get the metafile out?


Well, you've got an object of type MemoryStream. So one thing you
could try is passing that to Image.FromStream() and see what happens.

However, my experience suggests that that won't work either. You
should definitely try it, just in case something's changed since last
year. But otherwise, you may find this thread helpful:
http://groups.google.com/group/micr...read/thread/50cfd03caa3f31b9/2b9c42a12b6fb7e2


The short story: the Windows clipboard, metafiles, and .NET don't
really get along very well. You are likely to need to use p/invoke
(interop) to get this to work (and only with the "Enhanced Metafile"
format, not the older "Metafile" format).

Pete

I can't get any variants of this to work:
Image tmp = Image.FromStream((System.IO.MemoryStream)
copyObject.GetData("MetaFilePict"));

I'll check the PInvoke route....
 
P

Peter Duniho

I can't get any variants of this to work:
Image tmp = Image.FromStream((System.IO.MemoryStream)
copyObject.GetData("MetaFilePict"));

I was afraid that might be the case. It definitely was for me a year ago
when I tried. :(
I'll check the PInvoke route....

Good luck. I think there should be enough information in that thread to
answer the question, but if not I know I've got some working code around
here somewhere that I can post, if need be.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top