Created
February 8, 2013 15:54
-
-
Save posaunehm/4739893 to your computer and use it in GitHub Desktop.
Typical implementation for conversion between winform Bitmap and WPF BitmapSource
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
| [System.Runtime.InteropServices.DllImport("gdi32.dll")] | |
| public static extern bool DeleteObject(IntPtr hObject); | |
| public static BitmapSource ToWPFBitmap(this System.Drawing.Bitmap bitmap) | |
| { | |
| var hBitmap = bitmap.GetHbitmap(); | |
| BitmapSource source; | |
| try | |
| { | |
| source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( | |
| hBitmap, IntPtr.Zero, Int32Rect.Empty, | |
| BitmapSizeOptions.FromEmptyOptions()); | |
| } | |
| finally | |
| { | |
| DeleteObject(hBitmap); | |
| } | |
| return source; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment