Silverlight Windows Phone 7: Load Images From URL

silverlight, windows-phone-7

Solution

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);
}

Problem

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

Original source