我有一个 View ,它显示一个带有标题和评论的图像。
当我加载现有图像时,我使用以下代码:
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.FinalizeCreation()
中。 :
if ((CreateOptions & BitmapCreateOptions.IgnoreImageCache) != 0)
{
ImagingCache.RemoveFromImageCache(uri);
}
如果您指定
IgnoreImageCache
,但不要从 URI 加载,它会中断。只要摆脱那面旗帜;它仅适用于从 URL 加载时。
关于wpf - 创建 BitmapImage 会引发缺少键异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16675976/