0

i want to change in runtime the Background property and i have to set an ImageBrush for it. I have added some images as Resources on my project, and now i can use them as System.Drawing.Bitmap.

How can i convert System.Drawing.Bitmap into ImageBrush ?

Carpo23
  • 25
  • 1
  • 9

2 Answers2

1

If you are using the codebehind to set it, you can do it like this:

BitmapImage img; 

// get bitmapimage from resources and assign to img

ImageBrush brush = new ImageBrush();
brush.ImageSource = img;

myControl.Background = brush;

If you are using databinding you'd need to implement a ValueConverter

Kenneth
  • 28,294
  • 6
  • 61
  • 84
0

From How to create ImageBrush from System.Drawing.Image in WPF?:

var bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(),
                                                         IntPtr.Zero,
                                                         Int32Rect.Empty,
                                                         BitmapSizeOptions.FromEmptyOptions()
        );
bitmap.Dispose();
var brush = new ImageBrush(bitmapSource);

"This solution, however, doesnt free the memory of the handle. For information on how to remove the memory leak see WPF CreateBitmapSourceFromHBitmap() memory leak"

Community
  • 1
  • 1
Tim S.
  • 55,448
  • 7
  • 96
  • 122