Wpf 创建BitmapImage会引发缺少密钥异常
我有一个视图,显示一个图像,旁边有标题和注释。 当我加载现有图像时,我使用以下代码:Wpf 创建BitmapImage会引发缺少密钥异常,wpf,c#-4.0,Wpf,C# 4.0,我有一个视图,显示一个图像,旁边有标题和注释。 当我加载现有图像时,我使用以下代码: this.ArtifactIdentity = image.ArtifactIdentity; this.Comment = image.Comment; this.Name = image.Name; this.MediaIdentity = image.MediaIdentity; this.ImageArray = image
this.ArtifactIdentity = image.ArtifactIdentity;
this.Comment = image.Comment;
this.Name = image.Name;
this.MediaIdentity = image.MediaIdentity;
this.ImageArray = image.Image;
Image = new BitmapImage();
Image.BeginInit();
Image.CacheOption = BitmapCacheOption.None;
Image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
Image.StreamSource = new MemoryStream(this.ImageArray);
Image.EndInit();
当我执行EndInit()时,它抛出缺少参数键的异常。堆栈跟踪显示以下内容:
at System.Collections.Hashtable.ContainsKey(Object key)
at System.Collections.Hashtable.Contains(Object key)
at System.Windows.Media.Imaging.ImagingCache.RemoveFromCache(Uri uri, Hashtable table)
at System.Windows.Media.Imaging.BitmapImage.FinalizeCreation()
at System.Windows.Media.Imaging.BitmapImage.EndInit()
那么,有人能告诉我为什么我在使用代码时会遇到这个异常吗?我看到很多人成功地使用了这个代码,但我却遇到了这个异常???我不知所措 这是由WPF中的一个错误引起的,位于
BitmapImage.FinalizeRecreation()
:
如果指定IgnoreImageCache
,但不从URI加载,则会中断
把那面旗子扔掉;它仅在从URL加载时适用。我发现了一篇由Bradley Grainger撰写的文章,其中有一个加载BitmaImage导致的异常列表。SLaks所说的是正确的,但在Brad的文章中,他指出下一个例外(没有适合完成的成像组件…)经常出现在Windows7机器上。异常表明元数据以某种方式损坏 我的解决方案进行了一些测试以确认,但基本上,如果我获取字节流,将其保存到临时文件,然后使用该临时文件填充BitmapImage,我可以毫无例外地成功加载它
这不是最理想的解决方案,但它做了我需要的一件事:它毫无例外地显示图像 确切的异常消息是什么?当我从选项中删除它时,我得到一个新的异常:没有找到适合完成此操作的成像组件。我有bmp、jpg或png作为可能的源代码。这就是我得到这个异常的原因吗?@SASS_Shooter:这可能是因为它无法确定加载的格式。堆栈跟踪是什么?您正在加载什么格式?@SASS_Shooter:尝试
bitmap.CacheOption=BitmapCacheOption.OnLoad
;看到了吗,我对此投了赞成票,因为有时候你只需要一个乱七八糟的问题就可以解决问题,完成工作。我也不喜欢:)
if ((CreateOptions & BitmapCreateOptions.IgnoreImageCache) != 0)
{
ImagingCache.RemoveFromImageCache(uri);
}