Search code examples
silverlightwindows-phone-7image-loading

Loading image from URL in Windows Phone 7


I use below code to load image from URL in my winodws phone 7 application.

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

It is working fine for me. But image is loading asynchronously and by the time I want to show some kind of busy indicator there and if image does not exist on such URL then I want to show some default image. How can I achieve that?


Solution

  • I think if you are subscribed to the Image.ImageFailed Event you should be able to show to default image in case of a non-existing image.

    Conditions in which this event can occur include the following:

    1. File not found.
    2. Invalid (unrecognized or unsupported) file format.
    3. Unknown file format decoding error after upload.

    So something like this might work for you:

    image1.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(handlerImageFailed);
    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);
    
    void handlerImageFailed(object sender, ExceptionRoutedEventArgs e)
    {
         // Show the default image
    }