|
|
|
|
|
|
When and how to use the right BitmapCacheOption setting
by
Ben Vincent
|
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. |
|
|
|
How to Get Your 2008 TransRockies Photos
by
Ben Vincent
|
| I took over 1000 photos at the 2008 TransRockies but it’s been over a year since the event, so I no longer have them online. If you are interesting in seeing photos of your team please contact me and I’ll upload them to the site for your viewing pleasure and purchase if you like them. Thanks. [Read More] |
|
|
|
Reading Geotagging in Photos Using Windows Imaging Component
by
Ben Vincent
|
| In this blog I’m going to explain how Geotagging metadata can be read and written using Windows Imaging Component. First lets look at all the queries. These are the common ones, there are plenty more but I have not found any immediate need to use them. [Read More] |
|
|
|
Fotofly v0.1 available for Download on Codeplex
by
Ben Vincent
|
| After several years of evolving my photo metadata code I’m finally ready to put it online. Being the nice corporate citizen that I am, I’m using CodePlex and you can find Fotofly here for download. My somewhat lofty description of the project is: “A comprehensive C# library for reading, manipulating and writing metadata stored in jpg photos using WPF and the Windows Imaging Component. Includes support for Windows Live Photo Gallery People Tags, GPS Coordinates and most EXIF, XMP & IPTC properties.” Hopefully I’ll get lots of downloads and feedback to help improve it further. [Read More] |
|
|
|
Reading and Creating Exif Rationals
by
Ben Vincent
|
| Quite why Jeida who created Exif decided to use Rationals is beyond me but they did so you have to work with them if you’re playing with jpg metadata. In this blog post I’ll share my Rational class so you don’t have to go through all the pain I went through in creating it. It’s amusingly short now I look at it here but it took way too long to work it out. The class has two constructors, one for the value you’ve retrieved from BitmapMetadata, the second when you’re creating your own rationals. [Read More] |
|
|
|
Basic Editing of Photo Metadata Using Windows Imaging Component
by
Ben Vincent
|
| In my previous posts I’ve provided examples on using Windows Imaging Component to Read & Write jpg metadata. In this post I’ll explain how to use ContainsQuery, GetQuery, SetQuery and RemoveQuery. As an added bonus I’m going to use the IPTC address fields as my example. Whilst BitmapMetadata does provide some standard properties like Subject and Title, it is far from comprehensive. [Read More] |
|
|
|
Writing Photo Metadata Using Windows Imaging Component
by
Ben Vincent
|
| In this blog I’m going to build on my previous posting on Reading Metadata and explain how to write metadata stored in a jpg file using Windows Imaging Component. You can find all my blogs on Windows Imaging Component here. If you want to change any of the metadata, the first thing you have to do is make sure there’s room for your changes. This is done by adding padding to the metadata. [Read More] |
|
|
|
Reading Photo Metadata Using Windows Imaging Component
by
Ben Vincent
|
| I still see a lot of questions on the Internet and at work on how to read (& write) metadata in Photos. There are plenty of examples out there but they all appear to have some pitfalls. To be quite honest, the code I’ve been using for a number of years now is mature enough that it solves almost all the problems I’ve seen. This blog is the first in a series that document how to read (this blog), write and manipulate photo metadata. [Read More] |
|
|
|
|
|
|
This website, all photography & other content is Copyright © Ben Vincent. Unauthorised use of images is strictly prohibited.
Catalog Last Updated: Mon, 19 Jul 2010, 22:48:43 | Version v4.0.3861.38195
|
Powered by
|
|
|