简体   繁体   中英

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?

使用resized.DownloadFailed事件获取通知。

您也可以使用ImageFailed事件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM