views:

1396

answers:

2

In the following code below:

Image img = new Image();
img.Source = new BitmapImage(new Uri("http://someURL/somefilename.jpg", UriKind.Absolute));

how can I determine if the image successfully loaded (when there's a valid URI)? i.e., The URI is a valid format, but the file may not exist.

+1  A: 

Image has an ImageFailed event.

BitmapSource (base for BitmapImage) has an IsDownloading property, as well as DownloadProgress, DownloadCompleted, and DownloadFailed events.

Joel B Fant
A: 

If you run your example code above (with a valid url but invalid image file) you will get an exception thrown:

Error: Sys.InvalidOperationException: ImageError error #4001 in control 'Xaml1': AG_E_NETWORK_ERROR

So if you wrap your code in a try/catch block you can determine if the image loaded property or not.

Bryant
Actually, there is no exception thrown. I can link to garbage or to nothing at all and there is no exception.