C# WPF-按URL加载图像时出现异常

C# WPF-按URL加载图像时出现异常,c#,.net,wpf,image,bitmap,C#,.net,Wpf,Image,Bitmap,我无法通过URL将图像下载到BitmapImage中 在某些工作站上,这不起作用。我不知道为什么 例如,在我自己的工作站上,这工作正常。同样的操作系统,同样的网络 嗯。。我有一个小代码,它从Web服务器加载一些图像,并在ListView中显示它们 此代码部分处理下载: Application.Current.Dispatcher.Invoke((Action)(delegate { try

我无法通过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));
这是一个例外:

Ausnahme von HRESULT:0x80072EE4 bei System.Runtime.InteropServices.Marshal.ThroweExceptionForhr内部(Int32 errorCode,IntPtr errorInfo) bei System.Runtime.InteropServices.Marshal.ThroweExceptionForHR(Int32错误代码) bei MS.Win32.WinInet.get_InternetCacheFolder() bei System.Windows.Media.Imaging.BitmapDownload.BeginDownload(BitmapDecoder解码器、Uri Uri、RequestCachePolicy、uriCachePolicy、Stream-Stream) bei System.Windows.Media.Imaging.LateBoundBitmapDecoder..ctor(Uri baseUri、Uri Uri、流、BitmapCreateOptions createOptions、BitmapCacheOption cacheOption、RequestCachePolicy RequestCachePolicy) bei System.Windows.Media.Imaging.BitmapDecoder.CreateFromUriOrStream(Uri baseUri、Uri Uri、Stream、BitmapCreateOptions createOptions、BitmapCacheOption cacheOption、RequestCachePolicy uriCachePolicy、Boolean insertInDecoderCache) bei System.Windows.Media.Imaging.BitmapImage.FinalizeRecreation()文件 bei System.Windows.Media.Imaging.BitmapImage.EndInit()文件


有人能帮我吗?

从堆栈跟踪中可以看出,问题似乎是由于试图访问系统下载的图像缓存造成的。通过设置
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();

然而,这只是一个猜测。

TL;DR:检查注册表项
HKEY\U CURRENT\U USER\Software\Microsoft\Windows\CurrentVersion\Explorer\USER Shell Folders
并修复任何不存在/不可访问/其他无效路径-然后注销并重新登录

我也碰到了这个问题。因此,首先,
0x2ee4=12004=ERROR\u INTERNET\u INTERNAL\u ERROR
,因此HRESULT
0x80072EE4=HRESULT\u FROM\u WIN32(ERROR\u INTERNET\u INTERNAL\u ERROR)

这并不能告诉你太多,所以我不得不一路跟踪wininet.dll来找出答案。在我的例子中,错误源于
WININET!CCacheClientCounters::Initialize
,实际上是由于缓存和Cookies外壳文件夹被设置为无效值而导致的。您可以在注册表项
HKEY\U CURRENT\U USER\Software\Microsoft\Windows\CurrentVersion\Explorer\USER shell Folders
中找到shell文件夹设置。请注意,要使更改生效,您必须至少注销并再次登录


这对我来说是修复它的原因,但由于这个错误可能意味着任何可能出错的事情,这可能不是你的问题。我在wininet.dll中对缓存进行了大量初始化,唯一的日志记录是一些ETW跟踪,这些跟踪似乎与原始源代码中的行号有关,因此对Microsoft以外的任何人调试都没有多大帮助。

不幸的是,这不起作用。现在我得到了这个错误:bei System.Collections.Hashtable.ContainsKey(对象键)bei System.Collections.Hashtable.Contains(对象键)bei System.Windows.Media.Imaging.Imaging.ImagageCache.RemoveFromCache(Uri Uri,Hashtable表)bei System.Windows.Media.Imaging.Imaging.BitmapImage.FinalizeCreation()bei System.Windows.Media.Imaging.BitmapImage.EndInit()哪个异常?您刚刚显示了调用堆栈。无论如何,图像缓存似乎有问题。异常:HRESULT:0x80072EE4