c# - Copy Enhanced Metafile from clipboard and save it as an image -
i using follwing c# code copy image clipboard.
if (clipboard.containsdata(system.windows.dataformats.enhancedmetafile)) { /* taken http://social.msdn.microsoft.com/forums/windowsdesktop/en-us/a5cebe0d-eee4-4a91-88e4-88eca9974a5c/excel-copypicture-and-asve-to-enhanced-metafile*/ var img = (system.windows.interop.interopbitmap)clipboard.getimage(); var bit = clipboard.getimage(); var enc = new system.windows.media.imaging.jpegbitmapencoder(); var stream = new filestream(filename + ".bmp", filemode.create); enc.frames.add(bitmapframe.create(bit)); enc.save(stream); }
i took snippet here. control go in if condition. clipboard.getimage()
returns null. can please suggest going wrong in here?
i have tried following snippet
metafile metafile = clipboard.getdata(system.windows.dataformats.enhancedmetafile) metafile; control control = new control(); graphics grfx = control.creategraphics(); memorystream ms = new memorystream(); intptr iphdc = grfx.gethdc(); grfx.releasehdc(iphdc); grfx.dispose(); grfx = graphics.fromimage(metafile); grfx.dispose();
this doesn't work.
you can use user32.dll
via p/invoke below:
public const uint cf_metafilepict = 3; public const uint cf_enhmetafile = 14; [dllimport("user32.dll", charset = charset.auto, exactspelling = true)] public static extern bool openclipboard(intptr hwndnewowner); [dllimport("user32.dll", charset = charset.auto, exactspelling = true)] public static extern bool closeclipboard(); [dllimport("user32.dll", charset = charset.auto, exactspelling = true)] public static extern intptr getclipboarddata(uint format); [dllimport("user32.dll", charset = charset.auto, exactspelling = true)] public static extern bool isclipboardformatavailable(uint format);
now, can read metafile:
metafile emf = null; if (openclipboard(intptr.zero)) { if (isclipboardformatavailable(cf_enhmetafile)) { var ptr = getclipboarddata(cf_enhmetafile); if (!ptr.equals(intptr.zero)) emf = new metafile(ptr, true); } // must close ir, or locked closeclipboard(); }
my original requirement involves more handling of metafile, create memorystream
:
using (var graphics = graphics.fromimage(new bitmap(1,1,pixelformat.format32bppargb))) { var hdc = graphics.gethdc(); using (var original = new memorystream()) { using (var dummy = graphics.fromimage(new metafile(original, hdc))) { dummy.drawimage(emf, 0, 0, emf.width, emf.height); dummy.flush(); } graphics.releasehdc(hdc); // more stuff } }
Comments
Post a Comment