WPF - 加载URL图片时出现异常

3

我遇到一个问题,就是无法通过URL下载图像到BitmapImage中。

在一些工作站上这个功能无法使用,我也不知道为什么...

例如,在我的工作站上,这个功能能够正常工作。操作系统和网络环境都一样。

我有一段代码,从Web服务器加载一些图像,并在ListView中显示它们。

以下是处理下载的代码部分:

Application.Current.Dispatcher.Invoke((Action)(delegate
                   {
                       try
                       {
                           DTOBild dtoBild = new DTOBild();
                           BitmapImage small_image_bmp = new BitmapImage(new Uri( "http://our-own-server/images/99999999/" + bild));


                           dtoBild.bitmap = small_image_bmp;

                           this.listeBilder.Add(dtoBild);
                       }
                       catch (Exception ex)
                       {
                           log.schreibeFehler("Error at loading image. Stack is following ...");
                           log.schreibeFehler(ex.Message);
                           log.schreibeFehler(ex.StackTrace);

                       }
                   }));

代码在这行崩溃:

      BitmapImage small_image_bmp = new BitmapImage(new Uri( "http://our-own-server/images/99999999/" + bild));

这是一个异常:

HRESULT 异常: 0x80072EE4 在 System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo) 中引发异常 在 System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode) 中引发异常 在 MS.Win32.WinInet.get_InternetCacheFolder() 中调用 在 System.Windows.Media.Imaging.BitmapDownload.BeginDownload(BitmapDecoder decoder, Uri uri, RequestCachePolicy uriCachePolicy, Stream stream) 中调用 在 System.Windows.Media.Imaging.LateBoundBitmapDecoder..ctor(Uri baseUri, Uri uri, Stream stream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, RequestCachePolicy requestCachePolicy) 中调用 在 System.Windows.Media.Imaging.BitmapDecoder.CreateFromUriOrStream(Uri baseUri, Uri uri, Stream stream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, RequestCachePolicy uriCachePolicy, Boolean insertInDecoderCache) 中调用 在 System.Windows.Media.Imaging.BitmapImage.FinalizeCreation() 中调用 在 System.Windows.Media.Imaging.BitmapImage.EndInit() 中调用

有人能帮我吗?

2个回答

1
TL;DR: 检查注册表键 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders,修复任何不存在/无法访问/无效的路径 - 然后注销并重新登录。
我也遇到了这个问题。首先,0x2ee4 = 12004 = ERROR_INTERNET_INTERNAL_ERROR,因此 HRESULT 0x80072EE4 = HRESULT_FROM_WIN32(ERROR_INTERNET_INTERNAL_ERROR) 现在这并没有告诉你太多信息,所以我不得不一直跟踪 wininet.dll 才能找出原因。在我的情况下,错误实际上是由于我的缓存和 Cookie shell 文件夹某些设置为无效值导致的,具体来说是源自于 WININET!CCacheClientCounters::Initialize。您可以在注册表键 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders 中找到 shell 文件夹设置。请注意,您至少需要注销并重新登录才能使更改生效。
这是我解决问题的方法,但由于该错误可能意味着任何可能出错的事情,所以它可能不是你的问题。我在wininet.dll中RE了缓存初始化的很多内容,唯一的日志记录是一些似乎是原始源代码中引用行号的ETW跟踪,因此对于微软以外的任何人来说,它真的不会帮助太多调试。

0

根据堆栈跟踪,问题似乎是由于尝试访问系统下载的图像缓存而引起的。您可以尝试通过设置BitmapCreateOptions.IgnoreImageCache标志来绕过缓存加载图像:

var small_image_bmp = new BitmapImage();
small_image_bmp.BeginInit();
small_image_bmp.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
small_image_bmp.UriSource = new Uri("http://our-own-server/images/99999999/" + bild);
small_image_bmp.EndInit();

不过这只是猜测。


很遗憾这个不起作用。现在我遇到了这个错误:在System.Collections.Hashtable.ContainsKey(Object key)处 在System.Collections.Hashtable.Contains(Object key)处 在System.Windows.Media.Imaging.ImagingCache.RemoveFromCache(Uri uri, Hashtable table)处 在System.Windows.Media.Imaging.BitmapImage.FinalizeCreation()处 在System.Windows.Media.Imaging.BitmapImage.EndInit()处。 - Maximus1809
哪个异常?你刚刚展示了调用堆栈。无论如何,图像缓存似乎有些问题。 - Clemens
异常:HRESULT:0x80072EE4 - Maximus1809

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接