Correct photo orientation using EXIF data with C#

When processing photos, sometimes you want to re-orient the photo according the orientation recorded by the camera (such as the iPhone’s accelerometer) and stored in the EXIF meta data.  It’s easy to do:

// Rotate the image according to EXIF data
var bmp = new Bitmap(pathToImageFile);
var exif = new EXIFextractor(ref bmp, "n"); // get source from
if (exif["Orientation"] != null)
RotateFlipType flip = OrientationToFlipType(exif["Orientation"].ToString());
if (flip != RotateFlipType.RotateNoneFlipNone) // don't flip of orientation is correct
exif.setTag(0x112, "1"); // Optional: reset orientation tag
bmp.Save(pathToImageFile, ImageFormat.Jpeg);
// Match the orientation code to the correct rotation:
private static RotateFlipType OrientationToFlipType(string orientation)
switch (int.Parse(orientation))
case 1:
return RotateFlipType.RotateNoneFlipNone;
case 2:
return RotateFlipType.RotateNoneFlipX;
case 3:
return RotateFlipType.Rotate180FlipNone;
case 4:
return RotateFlipType.Rotate180FlipX;
case 5:
return RotateFlipType.Rotate90FlipX;
case 6:
return RotateFlipType.Rotate90FlipNone;
case 7:
return RotateFlipType.Rotate270FlipX;
case 8:
return RotateFlipType.Rotate270FlipNone;
return RotateFlipType.RotateNoneFlipNone;

5 thoughts on “Correct photo orientation using EXIF data with C#”

  1. Thanks – this saved me quite a bit of time today.
    Reset orientation code did not work for me – what gets written cannot be re-read by EXIFextractor. For me, this works

    int propertyId = 0x112;
    int propertyLen = 2;
    short propertyType = 3;
    byte[] propertyValue = { 1, 0 };
    exif.setTag(propertyId, propertyLen, propertyType, propertyValue);


  2. Thanks for the article, it really helped me. Both the orientation function but also because I found a nice EXIF library for .NET.

  3. You helped me save a days work providing code and a library able to extract EXIF info from a memory stream. Thanks! =o)

Leave a Reply