简体   繁体   English

如何检测BitmapImage无法加载?

[英]How do I detect a BitmapImage failing to load?

I have the following code to load an image from a url. 我有以下代码从URL加载图像。 If the url doesn't exist, a placeholder should be loaded instead. 如果网址不存在,则应加载一个占位符。

public BitmapImage Image
{
    get
    {
        if (m_image == null)
        {
            try
            {
                BitmapImage image = new BitmapImage();
                image.BeginInit();
                image.UriSource = new Uri(m_photoPath);
                image.DecodePixelHeight = s_imagePixelHeight;
                image.EndInit();
                m_image = image;
            }
            catch (FileNotFoundException)
            {
                BitmapImage image = new BitmapImage();
                image.BeginInit();
                image.UriSource = new Uri(c_placeholderImagePath);
                image.DecodePixelHeight = s_imagePixelHeight;
                image.DecodePixelWidth = s_imagePixelWidth;
                image.EndInit();
                m_image = image;
            }
        }
        return m_image;
    }
}

I'm getting the weirdest error - when m_photoPath is a url which causes a 404 in any browser, no exception is thrown. 我遇到了最奇怪的错误-当m_photoPath是一个在任何浏览器中导致404的网址时,都不会引发异常。 I've tried checking if the file exists by using an HttpWebRequest , but everytime I call [HttpWebRequest instance].GetResponse() , there's a really, really long timeout (possibly infinite - I haven't waited around to find out). 我尝试使用HttpWebRequest检查文件是否存在,但是每次我调用[HttpWebRequest instance].GetResponse() ,都会发生非常长的超时(可能是无限的-我没有等着找出[HttpWebRequest instance].GetResponse() )。 So HttpWebRequest isn't an option. 因此, HttpWebRequest不是一个选择。 Any ideas? 有任何想法吗?

If the image fails to download, the BitmapImage's DownloadFailed event fires. 如果图像下载失败,则触发BitmapImage的DownloadFailed事件。 No exception is thrown. 没有异常被抛出。 You can wire the event up anytime, though it would be pragmatic to do it before calling EndInit! 您可以随时联系事件,尽管在调用EndInit之前这样做很实用!

This couldn't really be handled by an exception, since something could give up on downloading the image at any arbitrary point in the future - there's nowhere really to put a try/catch block. 这实际上不能由异常处理,因为将来在任何时候都可以放弃下载映像的某些方法-真的没有地方放置try / catch块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 WPF/BitmapImage 快速加载和显示图像? - How to quickly load and display images with WPF/BitmapImage? 如何在Windows Phone 7上将BitmapImage的大小调整为50x50? - How do I resize a BitmapImage to 50x50 on Windows Phone 7? 如何将BitmapImage从内存保存到WPF C#中的文件中? - How do I save a BitmapImage from memory into a file in WPF C#? 如何将IRandomAccessStreamWithContentType转换为BitMapImage UWP? - How can I convert IRandomAccessStreamWithContentType to BitMapImage UWP? 如何将BitmapImage转换为Icon? - How can I convert BitmapImage to Icon? 如何将WritableBitmap转换为BitmapImage - How can I convert WritableBitmap to BitmapImage 如何将WriteableBitmap转换为BitmapImage? - How can I convert WriteableBitmap to BitmapImage? 如何将BitmapImage图像转换为数据,以便可以使用Windows Phone 7通过HTTP POST请求将其上传到Web? - How do I convert a BitmapImage image to data so I can upload it to the web on a HTTP POST request with Windows Phone 7? WPF:如何从另一个BitmapImage创建一个BitmapImage…? - WPF: How to create a BitmapImage… from another BitmapImage? 将BitmapImage加载到WriteableBitmap中,但不存在任何方法 - Load BitmapImage into WriteableBitmap but no method existing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM