Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

Metadata from Managed Code

4.33/5 (7 votes)
18 Sep 2009CPOL5 min read 87.8K   2.3K  
a custom viewer that would make it easy for everyone to see a picture and view and edit the metadata.

Introduction

My wife and I are into tracking down our ancestors. We have collected family photos from many family members, and hope to share them with all the family. I have scanned in hundreds of photos.

My goal has been to harness the power of metadata to place as much information about each photo into the file itself. That way, when we send someone a copy of the photo, they automatically have all the information we have concerning the contents of the photo.

There are five pieces of metadata that I use. They are Description, Subject, Title, Keywords (Tags), and Rating. These pieces of metadata can be seen through various programs.

You can use Windows Explorer through the Details mode of View. However, trying to get the folder to ‘retain’ the setup for this is impossible. Just about every time I access a folder with pictures, I have to re-select the columns I need to view these five items.

There are many graphics programs such as Picasa, Adobe Photoshop, and others that will allow you to view and edit some of these five items, but I have not found any that will handle them all.

Consequently, I have been working on a custom viewer that would make it easy for everyone to see a picture and view and edit the metadata. But first, I had to be able to read and write the metadata to the image file without affecting the quality of the image.

I have scoured the internet for the means to do that. Most articles only dealt with reading the metadata. The few that attempted to show how to write the metadata were only partially successful. I say ‘only partially successful’ because as any of you who has delved into this subject knows, there are many different protocols used to store the data. There is EXIF, IPTC, XMP to name a few. A single piece of metadata could be stored in each of these formats. So, if you want to insure your information can be read by as many programs as possible, you need to address them all. Adobe comes the closest, but you have to manually handle each protocol.

Only one article that I found seemed to have a simple approach of letting you deal with a single property name, and it handled the messy details of dealing with the various protocols behind the scenes. It was titled “Photo Metadata Policy”. It described accessing the data through the Windows Shell Property system. Using this method allows you to concentrate on the metadata and not have to worry about which schema or schemas it is stored under. The following is a quote from the web page:

Metadata (file properties) for photo files can be stored using multiple metadata schemas, in different data formats, and in different locations within a file. In Windows Vista™, the Microsoft® Windows® Shell provides a built-in property handler for photo file formats, such as JPEG, TIFF, and PNG, to simplify metadata retrieval.

When a piece of metadata is present in different underlying schemas, the built-in property handler determines which value to return.

This sounded great, just what I wanted. Unfortunately, it could only be accessed with C++. I can hardly spell it much less program in it. My language of preference is VB.NET. This sent me back to the internet searching for some managed code to access the Shell properties.

I searched far and wide and found nothing, nada, zilch. Then I noticed a new addition to the Start Page of Visual Studio. Occasionally, I do stop and read the items posted there. Not as often as I should though.

On 09 August of 2009, Microsoft released Windows API Code Pack v1.0 with the following description:

The Windows API Code Pack provides a source code library that can be used to access Windows features from managed code.

I installed the Code Pack and looked through the sample code. In the Samples folder, under PropertiesEditDemo, was just what I needed. It comes in both C# and VB flavors.

I set up a Windows Forms Application project to use that code. It was written as a console project. Re-coding it to a Forms App was the hardest piece of coding I had to do. I created a small class (SHPropertyEdit.vb) to interface between the demo form and the Code Pack. It was nothing exotic and the code should be self-explanatory.

The Code Pack only comes in C#, but, since I don’t need to alter it, that is just fine. After compiling the Code Pack, I moved Microsoft.WindowsAPICodePack.dll and Microsoft.WindowsAPICodePack.Shell.dll to a folder I named DLLs under the SHPropertyEdit folder. After referencing them in SHPropertyEdit and compiling the class, I was ready to go.

The demo form allows for me to locate a media file and perform one of four functions:

    Image 1

  1. Get – get the values for the five properties I am interested in.
  2. Set – the values in the textboxes are written to the file.

    Image 2

  3. Info – returns information about a single property. Type the name of the property in the filter textbox.

    Image 3

  4. Enum – returns the property values for all the properties contained in the file. You can type a single property name in the Filter textbox, or leave the textbox empty and retrieve them all.

I have to admit I have not done testing beyond the five properties I am interested in. But, I did try it out on many different file types: .jpg, .png, .tif, .doc, .wma, .pdf, .xls, .jpg, .dvr-ms, and .mpg.

I am an amateur programmer trying to learn to use .NET and comes up with some useful utilities. If you find anything I have done wrong or could do better, please keep that in mind and keep your criticism constructive.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)