1

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?

2 Answers 2

3

Use the resized.DownloadFailed event to get informed.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! It looks like it's been happening on DecodeFailed. Is there a way to display messages to the user from these handlers? It looks like they both happen out of the UI thread.
@Dan: Use something like: Dispatcher.BeginInvoke(new Action(() => { /*Her comes your action*/ }), DispatcherPriority.ApplicationIdle);
0

You can also use the ImageFailed event.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.