[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: Use EXIF information to orient an image in C#

[Use EXIF information to orient an image in C#]

The example Read an image file's EXIF orientation data in C# shows how to read EXIF orientation information from an image. Using that information, it's easy to orient the image so it appears right side up.

This example uses the following code to orient an image.

// Orient the image properly. public static void OrientImage(Image img) { // Get the image's orientation. ExifOrientations orientation = ImageOrientation(img); // Orient the image. switch (orientation) { case ExifOrientations.Unknown: case ExifOrientations.TopLeft: break; case ExifOrientations.TopRight: img.RotateFlip(RotateFlipType.RotateNoneFlipX); break; case ExifOrientations.BottomRight: img.RotateFlip(RotateFlipType.Rotate180FlipNone); break; case ExifOrientations.BottomLeft: img.RotateFlip(RotateFlipType.RotateNoneFlipY); break; case ExifOrientations.LeftTop: img.RotateFlip(RotateFlipType.Rotate90FlipX); break; case ExifOrientations.RightTop: img.RotateFlip(RotateFlipType.Rotate90FlipNone); break; case ExifOrientations.RightBottom: img.RotateFlip(RotateFlipType.Rotate90FlipY); break; case ExifOrientations.LeftBottom: img.RotateFlip(RotateFlipType.Rotate270FlipNone); break; } // Set the image's orientation to TopLeft. SetImageOrientation(img, ExifOrientations.TopLeft); } // Set the image's orientation. public static void SetImageOrientation(Image img, ExifOrientations orientation) { // Get the index of the orientation property. int orientation_index = Array.IndexOf(img.PropertyIdList, OrientationId); // If there is no such property, do nothing. if (orientation_index < 0) return; // Set the orientation value. PropertyItem item = img.GetPropertyItem(OrientationId); item.Value[0] = (byte)orientation; img.SetPropertyItem(item); }

The OrientImage method calls the ImageOrientation method described in the earlier example to see how the image is oriented. It then calls the Image class's RotateFlip method to rotate and flip the image so it is right side up. It finishes by calling the SetImageOrientation method to make the Image object's orientation TopLeft, the normal right side up orientation.

The SetImageOrientation method finds the index of the image's orientation ID property. If that ID is present, the method gets its PropertyItem and sets the item's Value to the new orientation. It then uses the Image object's SetPropertyItem method to set the new value.

After you reorient the image, you can save it into a file if you like.

The following code shows how the main program displays the original and oriented images.

// Open the file and read its orientation information. private void btnOpen_Click(object sender, EventArgs e) { // Open the file. using (Bitmap bm = new Bitmap(txtFile.Text)) { // Display the original image. Bitmap original_bm = new Bitmap(bm); picOriginal.Image = original_bm; // Display the image property oriented. // Note: If you use new Bitmap(bm) to make the copy, // then the EXIF properties are lost. Clone instead. Bitmap oriented_bm = (Bitmap)bm.Clone(); ExifStuff.OrientImage(oriented_bm); picOriented.Image = oriented_bm; } }

The code creates a Bitmap holding the image. It then makes a copy of the original image and displays it.

If the code makes a copy in this way, all of the EXIF information is lost so the program, can't orient the image. To avoid that, the program then clones the image and calls OrientImage to orient the clone. It then displays the result.

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

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