Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silverlight Windows Phone 7: Load Images From URL

I got the code below that is trying to load an image from the web into an Image control, when I run it I get an error on the given line that no network access is allowed:

private void button1_Click(object sender, RoutedEventArgs e)
        {
            WebClient webClientImgDownloader = new WebClient();
            webClientImgDownloader.OpenReadCompleted += new OpenReadCompletedEventHandler(webClientImgDownloader_OpenReadCompleted);
            webClientImgDownloader.OpenReadAsync(new Uri("http://dilbert.com/dyn/str_strip/000000000/00000000/0000000/000000/80000/5000/100/85108/85108.strip.print.gif", UriKind.Absolute));
        }

        void webClientImgDownloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            BitmapImage bitmap = new BitmapImage();
            bitmap.SetSource(e.Result); // ERROR HERE!
            image1.Source = bitmap;
        }

Silverlight for Windows Phone 7

like image 736
Lennie De Villiers Avatar asked Mar 16 '10 14:03

Lennie De Villiers


2 Answers

Trying to download content with WebClient will require a client access policy file to be present on the source server. For images you can avoid this requirement by doing it like this:-

private void button1_Click(object sender, RoutedEventArgs e)
{
    Uri uri = new Uri("http://dilbert.com/dyn/str_strip/000000000/00000000/0000000/000000/80000/5000/100/85108/85108.strip.print.gif", UriKind.Absolute)
    image1.Source = new BitmapImage(uri);
}
like image 145
AnthonyWJones Avatar answered Nov 16 '22 00:11

AnthonyWJones


I see you're retrieving the image from Dilbert.com does that site have a cross domain policy file?

like image 30
Graeme Bradbury Avatar answered Nov 16 '22 02:11

Graeme Bradbury