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:
        case ExifOrientations.TopRight:
        case ExifOrientations.BottomRight:
        case ExifOrientations.BottomLeft:
        case ExifOrientations.LeftTop:
        case ExifOrientations.RightTop:
        case ExifOrientations.RightBottom:
        case ExifOrientations.LeftBottom:

    // 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;

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();
        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 Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in files, graphics, image processing and tagged , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *