Reputation: 1451
I am using the follwing C# code to copy the image from clipboard.
if (Clipboard.ContainsData(System.Windows.DataFormats.EnhancedMetafile))
{
/* taken from 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 this snippet from here. The control does go in the if condition. Clipboard.GetImage()
returns null. Can someone please suggest what is going wrong in here?
I have also tried the following snippet
Metafile metafile = Clipboard.GetData(System.Windows.DataFormats.EnhancedMetafile) as 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 too doesn't work.
Upvotes: 5
Views: 5683
Reputation: 57976
You can use user32.dll
via p/invoke as 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, you can read your 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);
}
// You must close ir, or it will be locked
CloseClipboard();
}
My original requirement involves some more handling of that metafile, so I create a 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);
// do more stuff
}
}
Upvotes: 6
Reputation: 43023
The following code will work. You can change the format of the image you save if you want.
if (Clipboard.ContainsImage())
{
Image image = Clipboard.GetImage();
image.Save(@"c:\temp\image.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
Upvotes: 1