Reset Image Orientation using EXIF in C#

Let's say you took the photos using smart phones or regular cameras in different orientation and in different angles (rotate the camera to 90°, 270° degrees, etc). And now you copy the photos to system and observe that, the photo orientation is incorrect.

When you upload these photos to other sites to set profile picture or  album picture or timeline photo, etc., you will see that photos were uploaded with incorrect orientation.

So, sometimes we need to re-orient the photos as per the orientation recorded by cameras. The orientation information is stored in EXIF meta data of each photo.

The following C# code will correct the orientation, if you capture the photos from any device, in any angles..
            string sImage = @"E:\srinivas\DSC01.jpg";
            Image img = Image.FromFile(sImage);

            foreach (var prop in img.PropertyItems)
            {
                if (prop.Id == 0x0112) //value of EXIF
                {
                    int orientationValue = img.GetPropertyItem(prop.Id).Value[0];
                    RotateFlipType rotateFlipType = GetOrientationToFlipType(orientationValue);
                    img.RotateFlip(rotateFlipType);
                    img.Save(@"E:\srinivas\DSC01_modified.jpg");
                    break;
                }
            }

        private static RotateFlipType GetOrientationToFlipType(int orientationValue)
        {
            RotateFlipType rotateFlipType = RotateFlipType.RotateNoneFlipNone;

            switch (orientationValue)
            {
                case 1:
                    rotateFlipType = RotateFlipType.RotateNoneFlipNone;
                    break;
                case 2:
                    rotateFlipType = RotateFlipType.RotateNoneFlipX;
                    break;
                case 3:
                    rotateFlipType = RotateFlipType.Rotate180FlipNone;
                    break;
                case 4:
                    rotateFlipType = RotateFlipType.Rotate180FlipX;
                    break;
                case 5:
                    rotateFlipType = RotateFlipType.Rotate90FlipX;
                    break;
                case 6:
                    rotateFlipType = RotateFlipType.Rotate90FlipNone;
                    break;
                case 7:
                    rotateFlipType = RotateFlipType.Rotate270FlipX;
                    break;
                case 8:
                    rotateFlipType = RotateFlipType.Rotate270FlipNone;
                    break;
                default:
                    rotateFlipType = RotateFlipType.RotateNoneFlipNone;
                    break;
            }

            return rotateFlipType;
        }

15 comments:

  1. Great delivery. Great arguments. Keep up the amazing
    work.

    My webpage: lasertest

    ReplyDelete
  2. What Is Namespace used in this Code ????

    ReplyDelete
  3. What Is Namespace used in this Code ???

    ReplyDelete
  4. This rotates the image data, but leaves the metadata that says it's still rotated. If a browser or device tries to use the rotation data to display this image it will be rotated again. You should add

    img.RemovePropertyItem(0x0112);

    after the rotation, and before you save!

    -wilks

    ReplyDelete
  5. its not working if (prop.Id == 0x0112) //value of EXIF

    ReplyDelete
  6. So this is a great solution. Thanks for your work. After spending all this time doing this myself programatically my co-worker stumbled upon a mind blowing solution. This is something i could have used 10 years ago. On the fly image resizing / rotation:

    https://www.nuget.org/packages/ImageResizer/4.2.0

    ReplyDelete
    Replies
    1. Thank you very much KC Abramson, you are welcome..!

      Delete
  7. Very good job!
    But, when I implement this code on my website there is a problem in a Repeater control. Only the first 5 images will show, not the rest. Any thoughts?

    This code in itemdatabound:

    string sImage = Server.MapPath("~/" + imageBlock.Src);
    System.Drawing.Image img = System.Drawing.Image.FromFile(sImage);

    foreach (var prop in img.PropertyItems)
    {
    if (prop.Id == 0x0112) //value of EXIF
    {
    int orientationValue = img.GetPropertyItem(prop.Id).Value[0];
    RotateFlipType rotateFlipType = GetOrientationToFlipType(orientationValue);
    img.RotateFlip(rotateFlipType);
    img.Save(Server.MapPath("~/" + imageBlock.Src));
    break;
    }
    }

    ReplyDelete

Powered by Blogger.