Photography Metadata Extraction via C#

Photography Metadata Extraction via C#

I finally worked out how to read a wide array of metadata directly from image files using custom-written programming code.

February 25, 2024

Tags: Photography, Database Development

As my photography practice has evolved over the past few years, image metadata has become a cornerstone. Through gathering, crunching, and analyzing metadata, I’ve developed into a better photographer.

When I’m out and about with a film camera, I’m pretty good with writing down dates and times, my exposure settings, and other pertinent information. But with digital photography, the camera takes care of recording a wide array of metadata with an image file.

Considering how important that information is to me, extracting image metadata and storing it in a form that’s useful has become a top priority.

When I created my own custom-built photography database, I used the Windows Photos app to do that extraction for me. In a nutshell, I found a way to configure a routine that reads from that application’s SQLite metadata storage repository and loads it into my Microsoft SQL Server database. But running that routine has always been a little clumsy. I wanted a better way.

I researched the matter further and discovered that one can read that metadata programmatically without having to go through an intermediate application. I knocked the rust off my .NET coding skills, created a simple C# console application project in Visual Studio, and set out to write some code that functioned exactly the way I wanted it to.

By invoking the Windows file properties dialog box and select the Details tab, you’ll see that there’s a ton of information available about an image:

File properties dialog box in Windows

With the help of a post on a blog simply named “Mikes Ramblings,” I discovered how to read that metadata using the FileStream, BitmapSource, and BitmapMetadata classes. I used them to extract date taken, title, subject, rating, keyword, and camera model metadata from image files.

But there’s more to it than that. Way, way more.

The exchangeable image file format, or Exif—apparently it’s not “EXIF” in all caps—is, according to Wikipedia, “a standard that specifies formats for images, sound, and ancillary tags used by digital cameras (including smartphones), scanners and other systems handling image and sound files recorded by digital cameras.”

For a long time, I knew Exif metadata existed, but again I was dependent upon using the Windows Photos app to extract it from my image files.

I figured things out with some help from the web:

Code block
A sample block of code that interprets how exposure mode is recorded as an Exif property item into something more human-readable.

I spent days piecing together my code for extracting exposure, location, and other metadata. There’s a lot of trial and error that goes into software development like this. That proved to be all the more apparent to me in this case since writing code in C# is not my strong suit. But I like a challenge, and I knew that overcoming it would reap a lot of benefits.

My catalog of photography represents a record of my lifetime experiences, and I don’t take protecting that record lightly. Using a copy of image files to develop against, I tested and tested and tested some more before I was satisfied that the code I wrote would do no harm. I worked through the inevitable hiccups and eventually got to something I was comfortable running over all my photographs.

Still, I confess that it was a little terrifying to run this particular console application. I broke a bit of a sweat as I watched it loop over more than 24,000 image files. But in the end, everything turned out just fine.

With the mere click of a button, I can now extract new image metadata and load it into my photography database. It’s really satisfying to run an application, know precisely what it’s doing, and achieve an end result that is far better than what I had been using before.

Newer     Older