wpf - 创建 BitmapImage 会引发缺少键异常

标签 wpf c#-4.0

我有一个 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/

相关文章:

wpf - 如何在标签中添加换行符

c# - DynamicMethod.TryInvokeMember() 相对于使用字典作为参数的优势?

c# - 禁用 wpf 窗口上的鼠标事件和放置在其上的用户控件

c# - Catel ViewModelToModel 没有链接

wpf - 在WPF中更新Shapes值的最佳方法

c# - EF Core 执行sql

mysql - 防火墙后面的.NET SQL 查询身份验证

c# - 将 true/false 更改为图像

wpf - 覆盖 UserControl 中的 DependencyProperty

wpf - "Measure"的性能问题