Click here to Skip to main content
15,879,474 members
Articles / Web Development / ASP.NET
Tip/Trick

Image Orientation of Images Uploaded via ASP.NET fileupload Control

Rate me:
Please Sign up or sign in to vote.
5.00/5 (11 votes)
10 Feb 2014CPOL 40.4K   11   11
Image Orientation of Images Uploaded via ASP.NET fileupload Control

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:

C#
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: // landscape, do nothing
break;

case 8: // rotated 90 right
// de-rotate:
originalImage.RotateFlip(rotateFlipType: RotateFlipType.Rotate270FlipNone);
break;

case 3: // bottoms up
originalImage.RotateFlip(rotateFlipType: RotateFlipType.Rotate180FlipNone);
break;

case 6: // rotated 90 left
originalImage.RotateFlip(rotateFlipType: RotateFlipType.Rotate90FlipNone);
break;
}
}

As always, I hope this helps someone.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Denmark Denmark
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThank you but Pin
Member 146433893-Nov-19 16:40
Member 146433893-Nov-19 16:40 
QuestionThank you very much! Pin
Wolfgang Erdle17-Sep-18 3:08
Wolfgang Erdle17-Sep-18 3:08 
Questionit dose notwork for me! Pin
Member 1022744825-May-18 3:55
Member 1022744825-May-18 3:55 
PraiseBig thank you Pin
Bob C Tesch14-Feb-18 4:15
Bob C Tesch14-Feb-18 4:15 
QuestionTHANK YOU SO MUCH Pin
Member 1340418122-Jan-18 11:09
Member 1340418122-Jan-18 11:09 
PraiseThannnkkkkksssssss Pin
Member 1337182523-Aug-17 4:59
Member 1337182523-Aug-17 4:59 
QuestionMuch thanks! Pin
AMartinMpls12-Feb-17 16:12
AMartinMpls12-Feb-17 16:12 
PraiseThank you Pin
imsureshthapa7-Sep-16 18:28
professionalimsureshthapa7-Sep-16 18:28 
QuestionThanksssss Pin
Caio Carvalho13-Aug-15 2:26
Caio Carvalho13-Aug-15 2:26 
QuestionThank you Pin
Sitesource21-May-14 5:45
Sitesource21-May-14 5:45 
AnswerRe: Thank you Pin
harleydk21-May-14 9:47
harleydk21-May-14 9:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.