Created
March 30, 2016 11:22
-
-
Save ststeiger/5dd06ebaac90cf099ec116714bb90dc7 to your computer and use it in GitHub Desktop.
Get a System.Drawing.Image from the Windows Clipboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace TestWindowsApplicaton | |
| { | |
| public class ClipboardHelper | |
| { | |
| private const uint CF_METAFILEPICT = 3; | |
| private const uint CF_ENHMETAFILE = 14; | |
| [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, ExactSpelling = true)] | |
| private static extern bool OpenClipboard(System.IntPtr hWndNewOwner); | |
| [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, ExactSpelling = true)] | |
| private static extern bool CloseClipboard(); | |
| [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, ExactSpelling = true)] | |
| private static extern System.IntPtr GetClipboardData(uint format); | |
| [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, ExactSpelling = true)] | |
| private static extern bool IsClipboardFormatAvailable(uint format); | |
| private static System.Drawing.Image GetGenericClipboardImage() | |
| { | |
| System.Drawing.Image img = null; | |
| if (System.Windows.Forms.Clipboard.ContainsImage()) | |
| { | |
| img = System.Windows.Forms.Clipboard.GetImage(); | |
| } | |
| return img; | |
| } // End Function GetGenericClipboardImage | |
| private static System.Drawing.Image GetClipboardBitmap() | |
| { | |
| System.Drawing.Bitmap bitmap = null; | |
| System.Windows.Forms.IDataObject data = System.Windows.Forms.Clipboard.GetDataObject(); | |
| if (data == null) | |
| return bitmap; | |
| if (data.GetDataPresent(System.Windows.Forms.DataFormats.Bitmap)) | |
| { | |
| bitmap = (data.GetData(System.Windows.Forms.DataFormats.Bitmap, true) as System.Drawing.Bitmap); | |
| } | |
| return bitmap; | |
| } // End Function GetClipboardBitmap | |
| // https://stackoverflow.com/questions/18602171/copy-enhanced-metafile-from-clipboard-and-save-it-as-an-image | |
| private static System.Drawing.Image GetClipboardMetafile() | |
| { | |
| System.Drawing.Imaging.Metafile emf = null; | |
| if (OpenClipboard(System.IntPtr.Zero)) | |
| { | |
| try | |
| { | |
| if (IsClipboardFormatAvailable(CF_ENHMETAFILE)) | |
| { | |
| System.IntPtr ptr = GetClipboardData(CF_ENHMETAFILE); | |
| if (!ptr.Equals(System.IntPtr.Zero)) | |
| emf = new System.Drawing.Imaging.Metafile(ptr, true); | |
| } | |
| } | |
| catch (System.Exception) | |
| { | |
| CloseClipboard(); | |
| throw; | |
| } | |
| // You must close ir, or it will be locked | |
| CloseClipboard(); | |
| } | |
| return emf; | |
| } // End Function GetClipboardMetafile | |
| public static System.Drawing.Image GetClipboardImage() | |
| { | |
| System.Drawing.Image img = null; | |
| img = GetGenericClipboardImage(); | |
| if (img != null) | |
| return img; | |
| img = GetClipboardBitmap(); | |
| if (img != null) | |
| return img; | |
| img = GetClipboardMetafile(); | |
| return img; | |
| } // End Function GetClipboardImage | |
| } // End Class ClipboardHelper | |
| } // End Namespace TestWindowsApplicaton |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment