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.
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
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With