Get ImageSource from Bitmap?
10,146
Nevermind, I figured it out.
public static Brush CreateBrushFromBitmap(Bitmap bmp)
{
return new ImageBrush(Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()));
}
Related videos on Youtube
Comments
-
mpen almost 2 years
I want to set a background image for my form/window like this guy but instead of an image file on disk I've got a
System.Drawing.Bitmap
in memory.I need to do something like this:
this.Background = new ImageBrush(new BitmapImage(bmp));
Except
BitmapImage
won't take aBitmap
, nor willImageBrush
and I'm not sure if any of the others will. There's one calledBitmapCacheBrush
but I don't know what that does. -
Alan over 9 yearsYou'll need to import and P/Invoke DeleteObject on that handle from Bitmap.GetHBitmap() or you'll be leaking GDI handles. Refer to the documentation for more info.