Jimmy Darji
Jimmy Darji

Reputation: 401

Get Image Orientation and rotate as per orientation

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

Upvotes: 40

Views: 48605

Answers (5)

Vizions007
Vizions007

Reputation: 1

This is what I use:

// pass in an image
ChkImage.ImageOrientation image = ChkImage.GetOrientation(picCrop.Image);


public enum ImageOrientation
{
  /// <summary>
  /// Image is correctly oriented
  /// </summary>
  Original = 1,
  /// <summary>
  /// Image has been mirrored horizontally
  /// </summary>
  MirrorOriginal = 2,
  /// <summary>
  /// Image has been rotated 180 degrees
  /// </summary>
  Half = 3,
  /// <summary>
  /// Image has been mirrored horizontally and rotated 180 degrees
  /// </summary>
  MirrorHalf = 4,
  /// <summary>
  /// Image has been mirrored horizontally and rotated 270 degrees clockwise
  /// </summary>
  MirrorThreeQuarter = 5,
  /// <summary>
  /// Image has been rotated 270 degrees clockwise
  /// </summary>
  ThreeQuarter = 6,
  /// <summary>
  /// Image has been mirrored horizontally and rotated 90 degrees clockwise.
  /// </summary>
  MirrorOneQuarter = 7,
  /// <summary>
  /// Image has been rotated 90 degrees clockwise.
  /// </summary>
  OneQuarter = 8
}



public static PropertyItem SafeGetPropertyItem(Image image, int propid)
{
  try
  {
    return image.GetPropertyItem(propid);
  }
  catch (ArgumentException)
  {
    return null;
  }
}


public static ImageOrientation GetOrientation(this Image image)
{
  PropertyItem pi = SafeGetPropertyItem(image, 0x112);

  if (pi == null || pi.Type != 3)
  {
    return ImageOrientation.Original;
  }

  return (ImageOrientation)BitConverter.ToInt16(pi.Value, 0);
}

Upvotes: 0

Dmitry Shashurov
Dmitry Shashurov

Reputation: 1734

Maybe you resizing of the image and at this moment you lose exif and you want to make a rotate image according to exif, in this case you just can copy exif into resized image:

const string inputFileName = "input.jpg";
const string outputFileName = "output.jpg";
var newSize = new Size(640, 480);
using (var bmpInput = Image.FromFile(inputFileName))
{
    using (var bmpOutput = new Bitmap(bmpInput, newSize))
    {
        foreach (int exif in bmpInput.PropertyIdList)
            bmpOutput.SetPropertyItem(bmpInput.GetPropertyItem(exif));
        bmpOutput.Save(outputFileName, ImageFormat.Jpeg);
    }
}

Upvotes: 1

saucecontrol
saucecontrol

Reputation: 1506

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);
        img.RemovePropertyItem(exifOrientationID);
    }
}

Upvotes: 70

Darkseal
Darkseal

Reputation: 9564

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#

Upvotes: 6

TH Todorov
TH Todorov

Reputation: 1159

You can use this link http://regex.info/exif.cgi to examine your image embedded metadata. If you don't find "0x0112" in the EXIF table, then the image does not contain rotation property.

Upvotes: 2

Related Questions