Click here to Skip to main content
15,868,141 members
Articles / Programming Languages / Visual Basic

Metadata from Managed Code

Rate me:
Please Sign up or sign in to vote.
4.33/5 (7 votes)
18 Sep 2009CPOL5 min read 84.5K   2.2K   31   35
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Help .. won't open in VS 2005 ! Pin
rctaubert17-Oct-09 1:26
rctaubert17-Oct-09 1:26 
GeneralThanks!!! I have been searching the Net now for four days exactly for this issue. [modified] Pin
McAkins10-Oct-09 4:50
McAkins10-Oct-09 4:50 
GeneralRe: Thanks!!! I have been searching the Net now for four days exactly for this issue. Pin
rctaubert11-Oct-09 1:37
rctaubert11-Oct-09 1:37 
QuestionNice job! Is this feature only available in Windows 7? Pin
HAROLD CHAO22-Sep-09 4:28
HAROLD CHAO22-Sep-09 4:28 
AnswerRe: Nice job! Is this feature only available in Windows 7? Pin
rctaubert22-Sep-09 4:35
rctaubert22-Sep-09 4:35 
GeneralRe: Nice job! Is this feature only available in Windows 7? Pin
jason728-Jun-10 3:26
jason728-Jun-10 3:26 
GeneralRe: Nice job! Is this feature only available in Windows 7? Pin
rctaubert28-Jun-10 3:57
rctaubert28-Jun-10 3:57 
GeneralRe: Nice job! Is this feature only available in Windows 7? Pin
jason728-Jun-10 4:28
jason728-Jun-10 4:28 
GeneralRe: Nice job! Is this feature only available in Windows 7? Pin
rctaubert28-Jun-10 4:48
rctaubert28-Jun-10 4:48 
GeneralNice discovery! Pin
John Whitmire21-Sep-09 11:19
professionalJohn Whitmire21-Sep-09 11:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.