开发者

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?


Use the resized.DownloadFailed event to get informed.


You can also use the ImageFailed event.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜