Joseph Michael Pesch
VP Programming

C# Auto-Rotate Image Based on Camera Rotation Exif Data

by 27. December 2013 08:14

This method uses ExifExtractor code referenced below from CodeProject.com.

public static Bitmap AutoRotateImage(Bitmap bmp)
{
  // Get source from: http://www.codeproject.com/KB/graphics/exifextractor.aspx?fid=207371
  var exif = new EXIFextractor(ref bmp, "n");
  switch (int.Parse(exif["Orientation"].ToString()))
  {
    case 1:
      bmp.RotateFlip(RotateFlipType.RotateNoneFlipNone);
      break;
    case 2:
      bmp.RotateFlip(RotateFlipType.RotateNoneFlipX);
      break;
    case 3:
      bmp.RotateFlip(RotateFlipType.Rotate180FlipNone);
      break;
    case 4:
      bmp.RotateFlip(RotateFlipType.Rotate180FlipX);
      break;
    case 5:
      bmp.RotateFlip(RotateFlipType.Rotate90FlipX);
      break;
    case 6:
      bmp.RotateFlip(RotateFlipType.Rotate90FlipNone);
      break;
    case 7:
      bmp.RotateFlip(RotateFlipType.Rotate270FlipX);
      break;
    case 8:
      bmp.RotateFlip(RotateFlipType.Rotate270FlipNone);
      break;
    default:
      bmp.RotateFlip(RotateFlipType.RotateNoneFlipNone);
      break;
  }
  return bmp;
}

 

Tags:

C#

Comments are closed