Blog | Photography | Highlights | Contact | About | Atom & RSS Feeds
When and how to use the right BitmapCacheOption setting
  • Blog
  • When and how to use the right BitmapCacheOption setting

I keep stumbling across the consequences of changing which BitmapCacheOption I use. Despite the fact that metadata in a file isn’t really that big in the grand scheme of things, changing which caching option you use can have annoying consequences. The two typical issues I run into are: running of our memory when process lots of files or not having any data to work with. Let’s look at the code:
// The Metadata we'll be returning
BitmapMetadata bitmapMetadata;
// Open the stream, readonly
BitmapCreateOptions createOptions = BitmapCreateOptions.PreservePixelFormat | BitmapCreateOptions.IgnoreColorProfile;
// Create a decoder, cache all content on load because we'll close the stream
using (Stream sourceStream = File.Open(file, FileMode.Open, FileAccess.Read))
{
    // Create a Bitmap Decoder, loading all metadata on load
    BitmapDecoder bitmapDecoder = BitmapDecoder.Create(sourceStream, createOptions, BitmapCacheOption.OnLoad);
    // Grab the metadata
    bitmapMetadata = bitmapDecoder.Frames[0].Metadata.Clone() as BitmapMetadata;
}
If you use BitmapCacheOption.OnLoad then the decoder will load all the metadata into memory from the stream. When you Clone the BitmapMetadata you can safely close the stream with a full copy. That sounds great but it’s not fast and I’ve seen 20Mb of memory allocated per jpg photo, so pretty soon you’re running out of memory even on a decent machine. But if you use BitmapCacheOption.None, when you clone the BitmapMetadata and exit the using statement, there’s no metadata to play with! So how do you make your code performant but still have data to use? Fotofly solves this by grabbing all the most frequently used data from BitmapMetadata before throwing the stream away. The code below works by passing BitmapMetadata into a class that provides additional methods for reading\writing common attributes. Using reflection all the data is then copied across to a class that has no reliance on BitmapMetadata.
// The Metadata we'll be returning
PhotoMetadata photoMetadata = new PhotoMetadata();
BitmapCreateOptions createOptions = BitmapCreateOptions.PreservePixelFormat | BitmapCreateOptions.IgnoreColorProfile; // Open the stream, readonly
using (Stream sourceStream = File.Open(file, FileMode.Open, FileAccess.Read))
{
    // Create a decoder with no cache options set
    BitmapDecoder bitmapDecoder = BitmapDecoder.Create(sourceStream, createOptions, BitmapCacheOption.None);
    // Create a new WpfMetadata class that exposes all the right fields
    WpfMetadata wpfMetadata = new WpfMetadata(bitmapDecoder.Frames[0].Metadata as BitmapMetadata);
    // Copy the common metadata across using reflection tool
    IPhotoMetadataTools.CopyMetadata(wpfMetadata, photoMetadata);
}
Even this doesn’t work when you’re loading hundreds of photos because Garbage Collection never appears to kick in. Under those circumstances I’ve found forcing Garbage Collection keeps the memory usage pretty low:
// Force Garbage Collection
GC.Collect();
GC.WaitForPendingFinalizers();
You can download Fotofly from Codeplex.

This website, all photography & other content is Copyright © Ben Vincent. Unauthorised use of images is strictly prohibited.
Last Updated: Wed, 14 Dec 2011, 16:30:58    |    Website Version v4.0.4138.41239    |    Content v7.002