WPF CANNOT IMPLICITLY CONVERT SYSTEM.DRAWING.BITMAP TO MEDIA.BRUSH

jayprakash sharma

Total Post:117

Points:821
Posted by  jayprakash sharma
WPF 
WPF
 3239  View(s)
Ratings:
Rate this:

I want to change the background of a button manually in my WPF app.

I have an image imported into my resources and I want to do:

MyButton.Background = MyProject.Properties.Resources.myImage;

But I get the error:

cannot implicitly convert system.drawing.bitmap to media.brush

How can I do this??

  1. ezra heywood

    Post:145

    Points:1019
    Re: WPF cannot implicitly convert system.drawing.bitmap to media.brush

    Hi Jay,


    You can create a BitmapImage from a Pack URI to that file.

     you have to create an ImageBrush from the BitmapImage to set the Background property.

    var uri = new Uri("pack://application:,,,/images/myImage.jpg");

    var image = new BitmapImage(uri);

    MyButton.Background = new ImageBrush(image);

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!