Rotating images based upon exif image data

My current project involves using tablets to take picture and upload them to the server.

Once we started testing we noticed that the images were of different orientations.

We wanted the images to display the same so I have created the following rotation information:

#region exif image processing
public enum ExifOrientations : byte
{
    Unknown = 0,
    TopLeft = 1,
    TopRight = 2,
    BottomRight = 3,
    BottomLeft = 4,
    LeftTop = 5,
    RightTop = 6,
    RightBottom = 7,
    LeftBottom = 8,
}
 
public static System.IO.Stream RotateImageStream(System.IO.Stream stream, string filename)
{
    System.IO.Stream return_stream = stream;
 
    var fi = new System.IO.FileInfo(filename);
 
    var image_file = true;
 
    var image_format = System.Drawing.Imaging.ImageFormat.Jpeg;
    switch (fi.Extension)
    {
        case ".jpg":
        case ".jpeg":
            image_format = System.Drawing.Imaging.ImageFormat.Jpeg;
            break;
        case ".png":
            image_format = System.Drawing.Imaging.ImageFormat.Png;
            break;
        case ".gif":
            image_format = System.Drawing.Imaging.ImageFormat.Gif;
            break;
        case ".bmp":
            image_format = System.Drawing.Imaging.ImageFormat.Bmp;
            break;
        default:
            image_file = false;
            break;
    }
 
    if (image_file)
    {
        var image = System.Drawing.Image.FromStream(stream);
        var rotated_image = RotateImageByExif(image);
        var ms = new System.IO.MemoryStream();
 
        rotated_image.Save(ms, image_format);
        return_stream = ms;
    }
 
    return_stream.Position = 0;
 
    return return_stream;
}
 
public static System.Drawing.Image RotateImageByExif(System.Drawing.Image image)
{
    var orientation_property_id = 274;
    var pi = image.PropertyItems.Where(x => x.Id == orientation_property_id).FirstOrDefault();
    if (pi != null)
    {
        var orientation = (ExifOrientations)BitConverter.ToInt16(pi.Value, 0);
        switch (orientation)
        {
            case ExifOrientations.Unknown:
            case ExifOrientations.TopLeft:
                break;
            case ExifOrientations.TopRight:
                image.RotateFlip(System.Drawing.RotateFlipType.RotateNoneFlipX);
                break;
            case ExifOrientations.BottomRight:
                image.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
                break;
            case ExifOrientations.BottomLeft:
                image.RotateFlip(System.Drawing.RotateFlipType.RotateNoneFlipY);
                break;
            case ExifOrientations.LeftTop:
                image.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipX);
                break;
            case ExifOrientations.RightTop:
                image.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);
                break;
            case ExifOrientations.RightBottom:
                image.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipY);
                break;
            case ExifOrientations.LeftBottom:
                image.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipNone);
                break;
        }
    }
 
    return image;
}
 
#endregion

Comments are closed.