Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Image Orientation and rotate as per orientation

Tags:

c#

having problem in getting image orientation with below code

    string fileName = @"D:\...\...\01012015004435.jpeg";      int rotate = 0;     using (var image = System.Drawing.Image.FromFile(fileName))     {         foreach (var prop in image.PropertyItems)         {             if (prop.Id == 0x112)             {                 if (prop.Value[0] == 6)                     rotate = 90;                 if (prop.Value[0] == 8)                     rotate = -90;                 if (prop.Value[0] == 3)                     rotate = 180;                 prop.Value[0] = 1;             }         }     } 

and after get proper orientation i used to rotate image like

private static RotateFlipType OrientationToFlipType(string orientation) {     switch (int.Parse(orientation))     {         case 1:             return RotateFlipType.RotateNoneFlipNone;             break;         case 2:             return RotateFlipType.RotateNoneFlipX;             break;         case 3:             return RotateFlipType.Rotate180FlipNone;             break;         case 4:             return RotateFlipType.Rotate180FlipX;             break;         case 5:             return RotateFlipType.Rotate90FlipX;             break;         case 6:             return RotateFlipType.Rotate90FlipNone;             break;         case 7:             return RotateFlipType.Rotate270FlipX;             break;         case 8:             return RotateFlipType.Rotate270FlipNone;             break;         default:             return RotateFlipType.RotateNoneFlipNone;     } } 

but problem is in first code

prop.Id i always get [20625]

prop.Id == 20625 

so not satisfy the condition every time please let me know if any problem or other option

thanks

like image 662
Jimmy Darji Avatar asked Jan 08 '15 07:01

Jimmy Darji


People also ask

What is EXIF rotation?

Auto-rotation The EXIF orientation value is used by Photoshop and other photo editing software to automatically rotate photos, saving you a manual task.

What is image orientation mean?

Image Display Orientation. Display orientation refers to whether row number increases upward or downward. The orientation also affects the sense in which angles are measured. This is important to know because, as described below, images in different formats are displayed in different directions.

How do I change the orientation of a picture in HTML?

Syntax: transform: rotate(90deg);


2 Answers

There's probably enough information in the other answers and comments to put this all together, but here's a working code example.

This extension method will take a System.Drawing Image, read its Exif Orientation tag (if present), and flip/rotate it (if necessary).

private const int exifOrientationID = 0x112; //274  public static void ExifRotate(this Image img) {     if (!img.PropertyIdList.Contains(exifOrientationID))         return;      var prop = img.GetPropertyItem(exifOrientationID);     int val = BitConverter.ToUInt16(prop.Value, 0);     var rot = RotateFlipType.RotateNoneFlipNone;      if (val == 3 || val == 4)         rot = RotateFlipType.Rotate180FlipNone;     else if (val == 5 || val == 6)         rot = RotateFlipType.Rotate90FlipNone;     else if (val == 7 || val == 8)         rot = RotateFlipType.Rotate270FlipNone;      if (val == 2 || val == 4 || val == 5 || val == 7)         rot |= RotateFlipType.RotateNoneFlipX;      if (rot != RotateFlipType.RotateNoneFlipNone)         img.RotateFlip(rot); } 
like image 114
saucecontrol Avatar answered Sep 29 '22 21:09

saucecontrol


Use the following:

  • img.PropertyIdList.Contains(orientationId) to check if the Exif tag is present.
  • img.GetPropertyItem(orientationId) to get it (after the above check, otherwise you'll get an ArgumentException).
  • img.SetPropertyItem(pItem) to set it.

I wrote a simple helper class that does all that: you can check the full source code here.

Other info and a quick case-study is also available on the following post on my blog:

Change image orientation for iPhone and/or Android pics in NET C#

like image 27
Darkseal Avatar answered Sep 29 '22 22:09

Darkseal