Skip to content

Instantly share code, notes, and snippets.

@posaunehm
Created February 8, 2013 15:54
Show Gist options
  • Select an option

  • Save posaunehm/4739893 to your computer and use it in GitHub Desktop.

Select an option

Save posaunehm/4739893 to your computer and use it in GitHub Desktop.
Typical implementation for conversion between winform Bitmap and WPF BitmapSource
[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