简体   繁体   中英

Download Image via URL, then set it as a button background in C#

Using VS 2013 and developing for Windows Phone 8.1 with Silverlight in C#.

Contacted an API and got a URL that lets me view an image, I want to use that URL to download the image, and then set it as the background to a button.

Not really sure how to do this exactly. Do I have to save the picture to internal memory, and then retrieve it and somehow set it as a background to an image?

I'm pretty lost in this area.

So what you need to do is, create an ImageBrush object and load set its source property as a BitmapImage from the URL that you wannt to take a picture then st the ImageBursh as the background of your Button Object.

         private ImageBrush GetImage(){

            BitmapImage bi = new BitmapImage(new Uri("http://pic2.pbsrc.com/home/jan2015/winter.jpg"));

            var imageBrush = new ImageBrush
                {
                  ImageSource = bi
                };

            return imageBrush;
    }

And in your button click method, or in the page constructor if you want( in ctor it wont work unless you go with Async and Await call) assing the retrieved imagebrush as the background.

   private void btn_Click(object sender, RoutedEventArgs e)
    {
        btn.Background = GetImage();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM