Dan Monego
Dan Monego

Reputation: 10087

How can I tell if a WPF bitmap image has failed to load?

I'm using the following code to load an image from file into a bitmap image class to display to the user at a particular size:

                BitmapImage resized = new BitmapImage();
                FileStream fs = new FileStream(ImageSource, FileMode.Open);
                MemoryStream ms = new MemoryStream();
                fs.CopyTo(ms);
                fs.Close();
                resized.BeginInit();
                resized.CacheOption = BitmapCacheOption.OnDemand;
                resized.DecodePixelHeight = (int)(_imageBaseHeight * zoomRate);
                resized.DecodePixelWidth = (int)(_imageBaseWidth * zoomRate);
                resized.StreamSource = ms;
                resized.EndInit();
                ImageDisplay = resized;

The problem is that sometimes, on particularly large images, this will fail silently and display a blank image without raising an exception. Is there a flag that I can check after EndInit() to be sure the image has loaded?

Upvotes: 1

Views: 1843

Answers (2)

Pic Mickael
Pic Mickael

Reputation: 1274

You can also use the ImageFailed event.

Upvotes: 0

HCL
HCL

Reputation: 36775

Use the resized.DownloadFailed event to get informed.

Upvotes: 3

Related Questions