Reset Image Orientation using EXIF in C#
Let's say you took the photos using smart phones or regular cameras in different orientation and in different angles (rotate the camera to 90°, 270° degrees, etc). And now you copy the photos to system and observe that, the photo orientation is incorrect.
When you upload these photos to other sites to set profile picture or album picture or timeline photo, etc., you will see that photos were uploaded with incorrect orientation.
So, sometimes we need to re-orient the photos as per the orientation recorded by cameras. The orientation information is stored in EXIF meta data of each photo.
The following C# code will correct the orientation, if you capture the photos from any device, in any angles..
When you upload these photos to other sites to set profile picture or album picture or timeline photo, etc., you will see that photos were uploaded with incorrect orientation.
So, sometimes we need to re-orient the photos as per the orientation recorded by cameras. The orientation information is stored in EXIF meta data of each photo.
The following C# code will correct the orientation, if you capture the photos from any device, in any angles..
string sImage = @"E:\srinivas\DSC01.jpg"; Image img = Image.FromFile(sImage); foreach (var prop in img.PropertyItems) { if (prop.Id == 0x0112) //value of EXIF { int orientationValue = img.GetPropertyItem(prop.Id).Value[0]; RotateFlipType rotateFlipType = GetOrientationToFlipType(orientationValue); img.RotateFlip(rotateFlipType); img.Save(@"E:\srinivas\DSC01_modified.jpg"); break; } } private static RotateFlipType GetOrientationToFlipType(int orientationValue) { RotateFlipType rotateFlipType = RotateFlipType.RotateNoneFlipNone; switch (orientationValue) { case 1: rotateFlipType = RotateFlipType.RotateNoneFlipNone; break; case 2: rotateFlipType = RotateFlipType.RotateNoneFlipX; break; case 3: rotateFlipType = RotateFlipType.Rotate180FlipNone; break; case 4: rotateFlipType = RotateFlipType.Rotate180FlipX; break; case 5: rotateFlipType = RotateFlipType.Rotate90FlipX; break; case 6: rotateFlipType = RotateFlipType.Rotate90FlipNone; break; case 7: rotateFlipType = RotateFlipType.Rotate270FlipX; break; case 8: rotateFlipType = RotateFlipType.Rotate270FlipNone; break; default: rotateFlipType = RotateFlipType.RotateNoneFlipNone; break; } return rotateFlipType; }
Great delivery. Great arguments. Keep up the amazing
ReplyDeletework.
My webpage: lasertest
Thank you very much
ReplyDeleteYou are welcome
Deletethanks a lot!!!
ReplyDeleteYou are welcome
DeleteWhat Is Namespace used in this Code ????
ReplyDeleteWhat Is Namespace used in this Code ???
ReplyDeleteSystem.Drawing namespace
ReplyDeleteThis rotates the image data, but leaves the metadata that says it's still rotated. If a browser or device tries to use the rotation data to display this image it will be rotated again. You should add
ReplyDeleteimg.RemovePropertyItem(0x0112);
after the rotation, and before you save!
-wilks
Good Good!!!
ReplyDeleteits not working if (prop.Id == 0x0112) //value of EXIF
ReplyDeletegreat
ReplyDeleteSo this is a great solution. Thanks for your work. After spending all this time doing this myself programatically my co-worker stumbled upon a mind blowing solution. This is something i could have used 10 years ago. On the fly image resizing / rotation:
ReplyDeletehttps://www.nuget.org/packages/ImageResizer/4.2.0
Thank you very much KC Abramson, you are welcome..!
DeleteVery good job!
ReplyDeleteBut, when I implement this code on my website there is a problem in a Repeater control. Only the first 5 images will show, not the rest. Any thoughts?
This code in itemdatabound:
string sImage = Server.MapPath("~/" + imageBlock.Src);
System.Drawing.Image img = System.Drawing.Image.FromFile(sImage);
foreach (var prop in img.PropertyItems)
{
if (prop.Id == 0x0112) //value of EXIF
{
int orientationValue = img.GetPropertyItem(prop.Id).Value[0];
RotateFlipType rotateFlipType = GetOrientationToFlipType(orientationValue);
img.RotateFlip(rotateFlipType);
img.Save(Server.MapPath("~/" + imageBlock.Src));
break;
}
}