Created
October 17, 2016 07:58
-
-
Save nuitsjp/d9d3380a48277958c90c6926c77b616e to your computer and use it in GitHub Desktop.
Convert System.Drawing.Bitmap to System.Windows.Media.ImageSource
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
[DllImport("gdi32.dll", EntryPoint = "DeleteObject")] | |
[return: MarshalAs(UnmanagedType.Bool)] | |
public static extern bool DeleteObject([In] IntPtr hObject); | |
public static ImageSource ToImageSource(this Bitmap bmp) | |
{ | |
var handle = bmp.GetHbitmap(); | |
try | |
{ | |
return Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); | |
} | |
finally { DeleteObject(handle); } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment