Image Information: For C# and VB NET Students
In previous lessons, we've opened up an image and displayed it in a picture box. We've got information about that image that included the height and width of the image, the resolution and the pixel depth. In the last lesson, we set up a Sub/method to retreive ASCII metadata. In this lesson, we'll set up something called an enumeration and make a call to our Sub/method so that we can get the date and time a photo was taken. We'll also get the make and model of the camera or phone. First up, enumerations.
Enumeration in C# and VB Net
Enumerations are like lists of variables you build yourself. Each name on your list is given a value. This saves you from remembering values as you can just enter the enumeration name instead.
In C#, you set them up like this
In VB, the same enumeration would be this:
The name of this enumeration is BestDays. But, like variable names, you can call them almost anything you like.
By default, the first item on your list will be 0, the second item 1, the third 2, etc. You can add your own values, however. If your numbers are going to be sequential, you only need the first one. For example:
Friday = 1,
Now, the first name, Friday, has a value of 1. The second will automatically get a value of 2, the third 3, and so on.
(C# students be careful of the name: you need a comma after each one, except the last one.)
With this in mind, let's set up our own enumeration to hold those long numbers and hex values. Add this enumeration to your code in C#: (Put it right at the top, just below public partial class Form1 : Form.)
PropertyTagDateTime = 0x0132,
PropertyTagExifDTOrig = 0x9003,
PropertyTagEquipMake = 0x010F,
PropertyTagEquipModel = 0x0110
Here's your enumeration, VB Net students:
PropertyTagDateTime = 306
PropertyTagExifDTOrig = 36867
PropertyTagEquipMake = 271
PropertyTagEquipModel = 272
This enumeration is one we've called PropertyID. Let's see how to use it.
Get the Date and Time an Image was taken
We want to call our Sub/method into action, the GetPropItems one. We can do it from the JPEG section of the BtnLoad code. The JPEG section is this in C#:
imageType = "JPEG Image";
And this in VB Net:
If ImageFormat.Jpeg.Equals(img.RawFormat) Then
imageType = "JPEG Image"
We can add the call to our GetPropItems Sub/method just under the imageType line. We have three parameters set up in GetPropItems: an image, some text for a message, and an ID. The ID will come from our enumeration.
The first two parameters are easy enough:
GetPropItems(img, "Date and Time Taken: ",
So we pass in img, which holds our image. After a comma, we pass in some text, "Date and Time Taken: ". After another comma, we can add our enumeration. If you were to type the name of the enumeration, which is PropertyID, then a dot, you'd see a popup list. This one:
All the items we set up at the top of the code in our enumeration are on the list. Select an item from your list. Notice that the value for the name is displayed in a tooltip.
In C#, there's a further complication in that you need to convert the enumeration value to an integer. So you can't just do this:
GetPropItems(img, "Date and Time Taken: ", PropertyID.PropertyTagDateTime);
You need to convert it with a cast. It's this instead:
GetPropItems(img, "Date and Time Taken: ", (int)PropertyID.PropertyTagDateTime);
So the cast is done by typing (int) before the enumeration.
In VB Net, you don't need to cast. Add this instead:
GetPropItems(img, "Date and Time Taken: ", PropertyID.PropertyTagDateTime)
The top of your code, the part with the enumeration, should look like this in C#:
And this in VB Net:
And the call to your GetPropItems method should look like this in C#:
The call to your GetPropItems Sub should look like this in VB Net:
Give it a try. Run your program and click your button. Locate a JPEG image that you've taken with a camera or camera phone. You should see something like this appear in your text box:
Now that we have the date and time a photo was taken, we can get the make of the camera and the camera or phone model. We can reuse our GetPropItems Sub/method for this, along with those other two enumerations we set up.
Get Camera Maker and Model used to take a Photo
Let's use those other two enumerations on the list. We'll use them to get the camera maker and the camera model.
The only thing you need to do here is to make another call to GetPropItems. Here's the one for the camera maker in C#:
GetPropItems(img, "\r\nCamera Maker: ", (int)PropertyID.PropertyTagEquipMake);
And here it is in VB Net:
GetPropItems(img, Environment.NewLine & "Camera Maker: ", PropertyID.PropertyTagEquipMake)
(Note the new line and carriage returns in the second argument.)
And to get the camera or phone model, make another call. This in C#:
GetPropItems(img, "\r\nCamera Model: ", (int)PropertyID.PropertyTagEquipModel);
And this in VB Net:
GetPropItems(img, Environment.NewLine & "Camera Model: ", PropertyID.PropertyTagEquipModel)
The JPEG if statement part should look like this in C#:
And here's the VB Net version:
Run your program again. Load up a JPEG image that you've taken with a camera or phone. This time, the text box will display something like this:
There's a huge amount of metadata you can get from an image taken with a camera or phone. Most of the metadata is of a technical nature, though. As we've done the three most popular items you might want, we'll leave it there and move on. In the next lesson, we'll do the coding to get the colors in an image. We'll start with how to add form controls at run time.