Converting a BITMAP structure to a BitmapImage/BitmapSource compatible byte array?
See the question and my original answer on StackOverflowIf you only need a BitmapSource (or an ImageSource), you can use GDI's CreateBitmapIndirect function and WPF interop's Imaging.CreateBitmapSourceFromHBitmap Method , something like this:
static BitmapSource FromBITMAP(ref BITMAP bmp)
{
Int32Rect rect = new Int32Rect(0, 0, bmp.Width, bmp.Height);
IntPtr hbitmap = CreateBitmapIndirect(ref bmp);
if (hbitmap == IntPtr.Zero)
return null;
try
{
return Imaging.CreateBitmapSourceFromHBitmap(hbitmap, IntPtr.Zero, rect, BitmapSizeOptions.FromEmptyOptions());
}
finally
{
DeleteObject(hbitmap);
}
}
[DllImport("gdi32.dll", SetLastError = true)]
public static extern IntPtr CreateBitmapIndirect(ref BITMAP lpbm);
[DllImport("gdi32.dll", SetLastError = true)]
public static extern bool DeleteObject(IntPtr hObject);