You want to extract information from within the JPEG pictures your camera creates. You might want to do this, for instance, to rename the pictures based on the date and time they were taken.
Sample code folder: Chapter 10\JPEGInfo
Use the GetPropertyItem() method of the Bitmap class to extract header information from a JPEG file.
Each brand of camera seems to create and store different header information in the picture files it creates, so this solution may or may not work for you. This recipe's code is generalized enough so that even though you might not have documentation listing the properties by their access numbers, you can check this program's output to help determine what information is available.
The GetJpgInformation() function listed here gets a list of all property IDs from the picture's bitmap, calls GetPropertyItem() for each of these, and then formats the results into a string array as best it can, replacing some characters and zero bytes as required to prevent string-handling problems:
Public Shared Function GetJpgInformation( _ ByVal whichFile As String) As String ' ----- Retrieve the properties of a JPEG file. Dim bytesPropertyID As Byte( ) Dim stringPropertyID As String Dim loadedImage As System.Drawing.Bitmap Dim propertyIDs( ) As Integer Dim result As New System.Text.StringBuilder Dim counter As Integer Dim scanProperty As Integer ' ----- Retrieve the image and its properties. loadedImage = New System.Drawing.Bitmap(whichFile) propertyIDs = loadedImage.PropertyIdList ' ----- Examine each property. For Each scanProperty In propertyIDs ' ----- Convert the property to a string format. bytesPropertyID = loadedImage.GetPropertyItem( _ scanProperty).Value stringPropertyID = System.Text.Encoding.ASCII. _ GetString(bytesPropertyID) ' ----- Only retain characters in the printable ' ASCII range. For counter = 0 To 255 If counter < 32 Or counter > 127 Then If (stringPropertyID.IndexOf(Chr(counter)) _ <> -1) Then stringPropertyID = Replace(stringPropertyID, _ Chr(counter), "") End If End If Next counter ' ----- Display the property if it's reasonable. If (stringPropertyID.Length > 0) And _ (stringPropertyID.Length < 70) Then result.Append(scanProperty.ToString) result.Append(": ") result.AppendLine(stringPropertyID) End If Next scanProperty ' ----- Display the results. Return result.ToString End Function Public Shared Function GetString( _ ByVal sourceBytes As Byte( )) As String ' ----- Convert a byte array to a string, taking into ' account the terminating null character. Dim result As String result = System.Text.Encoding.ASCII.GetString(sourceBytes) If (result.EndsWith(vbNullChar) = True) Then _ result = result.Substring(0, result.Length - 1) Return result End Function
Call the GetJpgInformation() function directly with the path to a valid JPEG file to view the properties of the file:
Figure 10-12 shows a sample of the output produced by this code.
Figure 10-12. The information stored in a JPEG file
As you can see from the output, not all data items are usable, or even recognizable as readable ASCII text. Your output will probably vary depending on the camera or software used to create your image files. For your camera, you can use the date and time stamps as shown to help rename your picture files for easy chronological storage and access.