Convert BitmapImage to System.Windows.Media.Brush

10,291

Create an ImageBrush and use that as the background:

 ImageBrush ib = new ImageBrush();
 ib.ImageSource = bitmap;
 canvas.Background = ib;
Share:
10,291
JavaAndCSharp
Author by

JavaAndCSharp

Java and C# FTW!

Updated on June 16, 2022

Comments

  • JavaAndCSharp
    JavaAndCSharp almost 2 years

    How would I convert a BitmapImage to a System.Windows.Media.Brush?

    I have a BitmapImage imaginatively called bitmap, and I have a Canvas (also imaginatively titled) canvas.

    How would I set the value of canvas to the value of bitmap?

    I've tried canvas.Background = bitmap;, but that didn't work: image.Source = bitmap; works for images, but not Canvases: and

    ImageSourceConverter imgs = new ImageSourceConverter(); canvas.SetValue(Image.SourceProperty, imgs.ConvertFromString(bitmap.ToString()));

    didn't work either.

    All of these worked with images, however.

    Maybe something with bitmap.ToString() would work?

  • JavaAndCSharp
    JavaAndCSharp almost 13 years
    Wow... easy as copy-and-paste! Thanks!
  • keyboardP
    keyboardP almost 13 years
    Indeed. Just make sure you understand it ;)
  • Dev
    Dev over 9 years
    1) how to get height and width from ImageBrush, 2) how to apply gesture on ImageBrush ?