[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Read an image file's EXIF orientation data in C#

[Read an image file's EXIF orientation data in C#]

The EXchangeable Image File (EXIF) standard defines all sorts of information that a camera can add to a jpg file. For example, the camera may record latitude, longitude, altitude, date and time, focal length, shutter speed, and resolution. This example shows how to get an image's orientation information.

Note that not all cameras can set EXIF information and some that can may not unless you use the proper settings. The two pictures included with this example do contain EXIF orientation information so you can test the program.

The Image class (from which Bitmap inherits) provides three useful items for working with EXIF data: the PropertyIdList property, the GetPropertyItem method, and the SetPropertyItem method.

The PropertyIdList property returns an array of integers giving the IDs of the properties stored in the image. You can use Array.IndexOf to try to find the index of a particular property's ID in this array. If the image doesn't contain information for that property, then Array.IndexOf returns -1.

The GetPropertyItem method returns a PropertyItem object containing information about a property. You need to pass GetPropertyItem the ID of the item you want to retrieve.

The PropertyItem class has the properties Id (the property's ID), Len (the length in bytes of the property's value), Type (defines the type of data in the Value property), and Value (the value).

The SetPropertyItem method sets a property's value. Unfortunately there is no constructor for the PropertyItem class so you cannot create one from scratch. You can, however, get a PropertyItem object from an image and then modify it.

The orientation property's ID is 0x0112. The PropertyItem for the orientation property holds an array of bytes in its Value property, with the first byte giving the orientation. The following picture shows the meanings of the orientations demonstrated by the letter F.

[Read an image file's EXIF orientation data in C#]

The example program defines a static ExifStuff class to hold EXIF methods. Here's how it defines the orientation ID and the orientation values.

// Orientations. public const int OrientationId = 0x0112; public enum ExifOrientations { Unknown = 0, TopLeft = 1, TopRight = 2, BottomRight = 3, BottomLeft = 4, LeftTop = 5, RightTop = 6, RightBottom = 7, LeftBottom = 8, }

The following code shows how the program gets the image's orientation.

// Return the image's orientation. public static ExifOrientations ImageOrientation(Image img) { // Get the index of the orientation property. int orientation_index = Array.IndexOf(img.PropertyIdList, OrientationId); // If there is no such property, return Unknown. if (orientation_index < 0) return ExifOrientations.Unknown; // Return the orientation value. return (ExifOrientations) img.GetPropertyItem(OrientationId).Value[0]; }

This code uses Array.IndexOf to see if the image's property ID list includes orientation. If the orientation ID is found, the method uses GetPropertyItem to get the corresponding PropertyItem object, gets its first Value entry, converts it into an ExifOrientations value, and returns the result.

The main program uses the ExifStuff methods in the following code to display an image, its orientation, and an image that displays the letter F with various orientations. (See the example Show sample EXIF orientations in C# to learn how that F image was made.)

// Open the file and read its orientation information. private void btnOpen_Click(object sender, EventArgs e) { // Open the file. Bitmap bm = new Bitmap(txtFile.Text); picOriginal.Image = bm; // Get the PropertyItems property from image. ExifStuff.ExifOrientations orientation = ExifStuff.ImageOrientation(bm); lblOrientation.Text = orientation.ToString(); picOrientation.Image = ExifStuff.OrientationImage(orientation); }

The code opens a JPG file and displays it. Next it then calls ExifStuff.ImageOrientation to get the image's orientation and displays it as a string. It finishes by calling ExifStuff.OrientationImage to get the oriented sample image and it displays the returned image.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.