Click here to Skip to main content
12,951,732 members (78,045 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


9 bookmarked
Posted 10 Feb 2014

Image Orientation of Images Uploaded via ASP.NET fileupload Control

, 10 Feb 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

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

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

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

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

As always, I hope this helps someone.


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


About the Author

Denmark Denmark
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionMuch thanks! Pin
AMartinMpls12-Feb-17 16:12
memberAMartinMpls12-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
memberCaio Carvalho13-Aug-15 2:26 
QuestionThank you Pin
Sitesource21-May-14 5:45
memberSitesource21-May-14 5:45 
AnswerRe: Thank you Pin
harleydk21-May-14 9:47
memberharleydk21-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 10 Feb 2014
Article Copyright 2014 by harleydk
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid