Image Metadata

Image Information: For C# and VB NET Students

 

When you take an image with your camera it will store metadata that you can retrieve. There can be a lot of metadata in the image. The date the picture was taken is just one piece of metadata you can get with code. (You'll see how to get the camera maker and the model in the next lesson.) The coding is quite tricky, though.

In C#, add a private void method to the code you have so far. This one:

private void GetPropItems(Image img, string message, int ID){
}

In VB Net, add this Sub:

Private Sub GetPropItems(img As Image, message As String, ID as Integer)
End Sub

Add a try catch statement. This one in C#:

try {
}
catch (Exception)
{

txtImageInfo.Text += message + "Not Available";

}

And this in VB Net:

Try
Catch ex As Exception

txtImageInfo.Text &= message & "Not Available"

End Try

As the first line of the try part, we need something called a PropertyItem object. This is used for the metadata of an image. You then use a Function/method of your PropertyItem object called GetPropertyItem. You need to hand a value over to GetPropertyItem, known as an ID. The value (ID) you hand over depends on what metadata you're trying to grab from the image. Here's the line to add:

C#

PropertyItem propItem = img.GetPropertyItem(ID);

VB Net

Dim propItem As PropertyItem = img.GetPropertyItem(ID)

So this creates a PropertyItem object called propItem. We're handing it the ID we passed in between the round brackets of GetPropItems.

The ID we're using can be found on this page:

Property Item IDs

There's a more detailed list here (slow-loading page warning):

Property Item Details

Let's take an example. There's this one on the first link above:

0x0132 PropertyTagDateTime

So the ID is 0x0132 and the Property Tag is PropertyTagDateTime.

However, there's also this one:

0x9003 PropertyTagExifDTOrig

Both of these IDs will get you a date. The first one gets "Date and time the image was created". While the second one gets you the "Date and time when the original image data was generated. For a digital camera, the date and time when the picture was taken".

What this means for you as a programmer is that you can add either 0x0132 or 0x9003 between the round brackets of GetPropertyItem. However, a further complication is that VB Net won't accept hexadecimal values like 0x0132 or 0x9003. You can do it, but it's easier to convert the hexadecimal values to decimal. 0x0132 is 306 in decimal. 0x9003 is 36867 in decimal.

What we're going to do later is to set up an enumeration. These will hold these hex or decimal values, which will save you from remembering them all.

 

Image Property Tags

The property tags come with a Type. The type for the property ID 0x0132 is PropertyTagTypeASCII. The property ID 0x9003 is also PropertyTagTypeASCII. It's that ASCII on the end that's the important part. It means you have to convert the property item value from ASCII to something you can use in your code.

In C#, add this if statement to your GetPropItems method:

if (propItem != null)
{
}

Add this if statement in VB Net:

If propItem IsNot Nothing Then
End If

We're just checking to make sure there is actually something stored in our propItem object.

Now set up an ASCIIEncoding object like this in C#:

ASCIIEncoding encod = new ASCIIEncoding();

And this in VB Net:

Dim encod As ASCIIEncoding = New ASCIIEncoding()

So we now have an ASCIIEncoding object called encod.

We now need to set up a string to get the information from the propItem value. Add this line in C#:

string asciiInfo = encod.GetString(propItem.Value, 0, propItem.Len);

And this in VB Net

Dim asciiInfo As String = encod.GetString(propItem.Value, 0, propItem.Len)

The ASCIIEncoding object encod has a Function/method called GetString. In between the round brackets of GetString you need three things: some bytes to convert, a starting position in your bytes, and how many bytes you want to grab. Our bytes are stored in propItem.Value. Type a 0 to get the starting position and propItem.Len to get byte count.

Finally, we can put the date into the text box. Here's the line to add in C#:

txtImageInfo.Text += message + asciiInfo;

And here it is in VB Net

txtImageInfo.Text &= message + asciiInfo

The whole of your method should look like this in C#:

C# code to get the property item for an image

And this in VB Net:

Visual Basic .Net code to get the property item for an image

Almost time to try it out. What we'll do first, though, is to set up an enumeration. We can store those awkward hex values or decimal number in our enumerated fields. Let's see how that works in the next lesson below.

Enumeration, Get Date Taken, Camera Details >>

Back to the C# NET Contents Page

Back to the VB NET Contents Page