Even though the pictures you take with your smartphone or such are in portrait orientation, when you transfer them to other systems, you can't be sure their orientation will come out right. This happened to me with my iPhone: images I took when uploaded via the fileUpload control were presented in a landscape fashion, despite being shot in the portrait orientation.
The trick in getting the images back to normal is to read the EXIF orientation information off the images's property list. Below, I'll demonstrate how to do this:
byte[] imageData = new byte[fileUpload.ContentLength];
fileUpload.InputStream.Read(imageData, 0, fileUpload.ContentLength);
MemoryStream ms = new MemoryStream(imageData);
Image originalImage = Image.FromStream(ms);
if (originalImage.PropertyIdList.Contains(0x0112))
{
int rotationValue = originalImage.GetPropertyItem(0x0112).Value[0];
switch (rotationValue)
{
case 1:
break;
case 8:
originalImage.RotateFlip(rotateFlipType: RotateFlipType.Rotate270FlipNone);
break;
case 3:
originalImage.RotateFlip(rotateFlipType: RotateFlipType.Rotate180FlipNone);
break;
case 6:
originalImage.RotateFlip(rotateFlipType: RotateFlipType.Rotate90FlipNone);
break;
}
}
As always, I hope this helps someone.