Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get ImageSource from Bitmap?

Tags:

c#

wpf

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 a Bitmap, nor will ImageBrush and I'm not sure if any of the others will. There's one called BitmapCacheBrush but I don't know what that does.

like image 589
mpen Avatar asked Jan 27 '12 03:01

mpen


1 Answers

Nevermind, I figured it out.

public static Brush CreateBrushFromBitmap(Bitmap bmp)
{
    return new ImageBrush(Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()));
}

credit

like image 186
mpen Avatar answered Sep 18 '22 19:09

mpen