Search code examples
wpfc#-4.0

Creating BitmapImage throws missing key exception


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!


Solution

  • I found an article that was written by Bradley Grainger here and had a list of exceptions caused by loading a BitmapImage. What SLaks stated is correct but in Brad's article he stated that the next exception (No Imaging component suitable to complete...) is frequently showing up on Windows 7 machines. The exception suggests that the metadata is corrupted in some way.

    My solution took some testing to confirm but basically if I take the byte stream, save it to a temp file, then use that tempfile to populate the BitmapImage I can successfully load it without exceptions.

    It is not the most desired solution but it does the one thing that I needed: it displays the image without exceptions!