-2

I would like to set the background image for a button to an image I have from a URL; but the following code does not work:

var button = new Button();
Image image = new Image();
image.Source = new BitmapImage(new Uri("https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-folder-128.png", UriKind.Absolute));                
button = image;

Specifically "button = image" doesn't work, because Button isn't Image type.

How should I set an image to be the background image of a button?

MethodMan
  • 18,625
  • 6
  • 34
  • 52
marcin1102
  • 27
  • 2

2 Answers2

0

You can accomplish this by using WebClient to first download the image locally before displaying it in the Button control

using (WebClient c = new WebClient())
{
    c.DownloadFile("https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-folder-128.png", "D:/image.png");
}
button.Image = Image.FromFile("D:/image.png");

Where D:/image.png is the location you would like to save your image to

Tyler Benzing
  • 1,116
  • 11
  • 25
0

Ok, I've got a solution.

            var button = new Button();
            var image = new ImageBrush();
            image.ImageSource = new BitmapImage(new Uri(url, UriKind.Absolute));         
            button.Background = image;
marcin1102
  • 27
  • 2