6

In a mobile web application I am developing, users are allowed to take a picture with their camera and the camera image is uploaded to a server. The issue I am having is that on iOS devices, images get an EXIF Orientation tag associated with them such as "ROTATE 90 CW". This orientation tag causes the image to be displayed in an incorrect orientation when it is displayed. For example, if the user takes a picture of something with their iPhone in portrait orientation, the image appears to be rotated to landscape when viewed on the server. I want to correct this issue on the server-side using VB.Net so that I automatically detect the EXIF Orientation tag and if it is "ROTATE 90 CW" (or any other value that will make the image appear to be displayed incorrectly), then I want to automatically rotate the image to the correct orientation. In summary, I want the image on the server to appear exactly as it appeared when the user took the picture with their camera.

Can someone post the code that will do this? Thanks in advance.

Obi Wan
  • 969
  • 11
  • 26
  • are you asking for code that might take an Image and perhaps RotateFlip it, say like Rotate90?? – Ňɏssa Pøngjǣrdenlarp Oct 10 '13 at 21:42
  • Yes. The image on the server side is in the form of a JPEG file, so I want to open the JPEG file, rotate the image inside of it, and the write the rotated image back as a JPeg file using the same file name. That is the most basic operation, but beyond that I am looking for an algorithm in VB.Net 4.5 that can auto-correct any JPEG image it finds with an EXIF orientation tag such as the above. I have been trying to write this but have so far failed. – Obi Wan Oct 10 '13 at 21:59
  • .NET doesnt have EXIF support, so you will have to use one of the libraries around for reading EXIF data for that part. The rest, rotating the bitmap is pretty straight forward and native to .NET. – Ňɏssa Pøngjǣrdenlarp Oct 10 '13 at 22:06

2 Answers2

18

For anyone who needs this, I basically resolved the issue using this code in VB.Net. I found this to be just what I needed:

  Public Function TestRotate(sImageFilePath As String) As Boolean
    Dim rft As RotateFlipType = RotateFlipType.RotateNoneFlipNone
    Dim img As Bitmap = Image.FromFile(sImageFilePath)
    Dim properties As PropertyItem() = img.PropertyItems
    Dim bReturn As Boolean = False
    For Each p As PropertyItem In properties
      If p.Id = 274 Then
        Dim orientation As Short = BitConverter.ToInt16(p.Value, 0)
        Select Case orientation
          Case 1
            rft = RotateFlipType.RotateNoneFlipNone
          Case 3
            rft = RotateFlipType.Rotate180FlipNone
          Case 6
           rft = RotateFlipType.Rotate90FlipNone
          Case 8
           rft = RotateFlipType.Rotate270FlipNone
        End Select
      End If
    Next
    If rft <> RotateFlipType.RotateNoneFlipNone Then
      img.RotateFlip(rft)
      System.IO.File.Delete(sImageFilePath)
      img.Save(sImageFilePath, System.Drawing.Imaging.ImageFormat.Jpeg)
      bReturn = True
    End If
    Return bReturn

  End Function
Obi Wan
  • 969
  • 11
  • 26
2

For anyone interested... C# version.

public static bool TestRotate(string filePath)
{
    var rft = RotateFlipType.RotateNoneFlipNone;
    var img = Image.FromFile(filePath);
    var properties = img.PropertyItems;
    var value = false;

    foreach (var prop in properties.Where(i => i.Id == 274))
    {
        var orientation = BitConverter.ToInt16(prop.Value, 0);
        rft = orientation == 1 ? RotateFlipType.RotateNoneFlipNone :
                orientation == 3 ? RotateFlipType.Rotate180FlipNone :
                orientation == 6 ? RotateFlipType.Rotate90FlipNone :
                orientation == 8 ? RotateFlipType.Rotate270FlipNone :
                RotateFlipType.RotateNoneFlipNone;
    }

    if (rft != RotateFlipType.RotateNoneFlipNone)
    {
        img.RotateFlip(rft);
        File.Delete(filePath);
        img.Save(filePath, ImageFormat.Jpeg);
        value = true;
    }

    return value;
}
aswallows
  • 320
  • 1
  • 4
  • 9