How to convert metafile to image using Drag'n'Droping in Winform

I am developing a Winform application with the .NET 3.5 framework in C #. I would like the user to be able to drag and drop the image from Word 2007. Basically, the user opens docx, selects a picture and drags and drops them into his PictureBox.

I already did the same process with image files from my desktop and from Internet pages, but I can not solve my problem using my metafile. I did some research, but I did not find solutions to my problem.

Here is what I did in my Drag & Drop event:

private void PictureBox_DragDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.MetafilePict)){ Image image = new Metafile((Stream)e.Data.GetData(DataFormats.MetafilePict)); } } 

I can get a stream with this code: (Stream) e.Data.GetData (DataFormats.MetafilePict), but I don’t know how to convert it to a metafile or better an Image object.

If you have any ideas or solutions, I will be happy to read it.

Thanks,

+4
source share
3 answers

Here is a working example of Drag n Drop from Word (not for PowerPoint and Excel):

  static Metafile GetMetafile(System.Windows.Forms.IDataObject obj) { var iobj = (System.Runtime.InteropServices.ComTypes.IDataObject)obj; var etc = iobj.EnumFormatEtc(System.Runtime.InteropServices.ComTypes.DATADIR.DATADIR_GET); var pceltFetched = new int[1]; var fmtetc = new System.Runtime.InteropServices.ComTypes.FORMATETC[1]; while (0 == etc.Next(1, fmtetc, pceltFetched) && pceltFetched[0] == 1) { var et = fmtetc[0]; var fmt = DataFormats.GetFormat(et.cfFormat); if (fmt.Name != "EnhancedMetafile") { continue; } System.Runtime.InteropServices.ComTypes.STGMEDIUM medium; iobj.GetData(ref et, out medium); return new Metafile(medium.unionmember, true); } return null; } private void Panel_DragDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.EnhancedMetafile) & e.Data.GetDataPresent(DataFormats.MetafilePict)) { Metafile meta = GetMetafile(e.Data); Image image = meta; } } 

After that, you can use image.Save to save the image, or you can use it in a picture or other control.

+3
source

It seems to me that you need to call a new metafile (stream), since there is no .FromStream method in Metafile .

0
source

I'm still digging on the net to try to solve my problem differently. I hope I found this unanswered topic telling about my problem, but without an answer: Get drag-and-drop MS Word + DataFormats.EnhancedMetafile and MetafilePict images:

http://www.codeguru.com/forum/showthread.php?t=456722

I am working with another io to copy a floating image (an image stored in Shape, not InlineShape) with Word 2003 and pasting it into my winform. I can’t insert the link of the second source (due to my low reputation on this site), but I will do it if someone asks.

Thus, it is obvious that there is a common problem with the fact that you cannot access your metafile stored in the clipboard and Drag & Drop.

I still need to figure out how to get my metafile using Drag & Drop.

0
source

All Articles