4

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!

SASS_Shooter
  • 2,186
  • 19
  • 30

2 Answers2

12

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.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • When I remove that from the options I get a new exception:No imaging component suitable to complete this operation was found. I have either bmp, jpg, or png as the possible source. Is this why I get this exception? – SASS_Shooter May 21 '13 at 17:50
  • @SASS_Shooter: That might be because it can't figure out what format to load as. What's the stack trace? What format are you loading? – SLaks May 21 '13 at 18:09
  • @SASS_Shooter: Try `bitmap.CacheOption = BitmapCacheOption.OnLoad`; see http://stackoverflow.com/a/5346766/34397 – SLaks May 21 '13 at 18:10
1

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!

SASS_Shooter
  • 2,186
  • 19
  • 30
  • I'm upvoting this because sometimes a kludge is all you need to get past the problem and get the work done. I don't like it either :) – John Faulkner Nov 21 '14 at 02:22