Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating BitmapImage throws missing key exception

Tags:

wpf

c#-4.0

I have a view that displays an image with it's title and comment alongside it. When I load up existing images I utilize this code:

        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();

When I execute the EndInit() it throw the exception missing parameter key. The stack trace shows this:

  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()

So can anyone tell me why I am getting this exception when I am using code I've seen many others use with success and yet I get this exception??? I'm at a loss!

like image 549
SASS_Shooter Avatar asked Dec 11 '22 14:12

SASS_Shooter


1 Answers

This is caused by a bug in WPF, in BitmapImage.FinalizeCreation():

if ((CreateOptions & BitmapCreateOptions.IgnoreImageCache) != 0)
{
    ImagingCache.RemoveFromImageCache(uri); 
}

If you specify IgnoreImageCache, but don't load from a URI, it breaks.

Just get rid of that flag; it only applies when loading from URLs.

like image 189
SLaks Avatar answered Jan 03 '23 03:01

SLaks