Image orientation and rotation to match orientation

image orientation problem 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 correct orientation i used to rotate the 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 the problem is in the first code

prop.Id

I always get [20625]

prop.Id == 20625

      

so do not meet the condition every time please let me know if you have any problems or other options.

thank

+13


source to share


3 answers


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

This extension method will accept 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);
}

      

+11


source


Use the following:

  • img.PropertyIdList.Contains(orientationId)

    to check for the presence of an Exif tag.
  • img.GetPropertyItem(orientationId)

    to get it (after checking above, otherwise you will receive ArgumentException

    ).
  • img.SetPropertyItem(pItem)

    to install it.

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



Other information and a short case study are also available in the following post on my blog:

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

+4


source


You can use this link http://regex.info/exif.cgi to view the embedded image metadata. If you do not find "0x0112" in the EXIF ​​table, then the image will not contain the rotation property.

+1


source







All Articles