11

I have a simple problem: When I load an image to a windows form PictureBox some pictures are rotated and others are not.

Basically, a user selects a picture with an OpenFileDialog and when the picture is selected:

private void OpenFD_FileOk(object sender, CancelEventArgs e)
{
    Image image = Image.FromFile(openFD.FileName);
    PB_profile.Image = image;
}

And yes I checked the original image rotation

EDIT:
I changed the PictureBox property SizeMode to StretchImage

TaW
  • 53,122
  • 8
  • 69
  • 111
Elliott Addi
  • 370
  • 4
  • 18

2 Answers2

14

If the pictures contains exif data the PropertyItems should include the orientation tag.

It encodes the rotation/flipping necessary to display the image correctly:

PropertyTagOrientation

Image orientation viewed in terms of rows and columns.

Tag 0x0112

1 - The 0th row is at the top of the visual image, and the 0th column is the visual left side.
2 - The 0th row is at the visual top of the image, and the 0th column is the visual right side.
3 - The 0th row is at the visual bottom of the image, and the 0th column is the visual right side.
4 - The 0th row is at the visual bottom of the image, and the 0th column is the visual left side.
5 - The 0th row is the visual left side of the image, and the 0th column is the visual top.
6 - The 0th row is the visual right side of the image, and the 0th column is the visual top.
7 - The 0th row is the visual right side of the image, and the 0th column is the visual bottom.
8 - The 0th row is the visual left side of the image, and the 0th column is the visual bottom.

Here is a function to retrieve a PropertyItem:

PropertyItem getPropertyItemByID(Image img, int Id)
{
  return 
    img.PropertyItems.Select(x => x).FirstOrDefault(x => x.Id == Id);
}

Here is an example of using the GDI+ RotateFlip method to adjust an image on the fly:

void Rotate(Bitmap bmp)
{
    PropertyItem pi = bmp.PropertyItems.Select(x => x)
                         .FirstOrDefault(x => x.Id == 0x0112);
    if (pi == null) return; 

    byte o = pi.Value[0];

    if (o==2) bmp.RotateFlip(RotateFlipType.RotateNoneFlipX);
    if (o==3) bmp.RotateFlip(RotateFlipType.RotateNoneFlipXY);
    if (o==4) bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);
    if (o==5) bmp.RotateFlip(RotateFlipType.Rotate90FlipX);
    if (o==6) bmp.RotateFlip(RotateFlipType.Rotate90FlipNone);
    if (o==7) bmp.RotateFlip(RotateFlipType.Rotate90FlipY);
    if (o==8) bmp.RotateFlip(RotateFlipType.Rotate90FlipXY);
}

It changes the image to the correctly rotated version..

I have tested to values with this nice set of sample images.

Note: The code will only work if the images actually contain the orientation tag. If they don't, maybe because they are scans, then it will do nothing.

Note 2 You wrote I checked the original image rotation. This is not so simple: The explorer will display the images already rotated, so here they all look right and even inspecting the properties doesn't reveal the orientation!

Usually, when no exif data are present, the PropertyTagOrientation tag is present but only has the default value of 1..

Update: If the image doesn't have the PropertyTagOrientation here is how you can add one:

    using System.Runtime.Serialization;
    ..

    pi = (PropertyItem)FormatterServices
        .GetUninitializedObject(typeof(PropertyItem));

    pi.Id = 0x0112;   // orientation
    pi.Len = 2;
    pi.Type = 3;
    pi.Value = new byte[2] { 1, 0 };

    pi.Value[0] = yourOrientationByte;

    yourImage.SetPropertyItem(pi);

Kudos to @ne1410s's excellent answer here!.

Note that adding PropertyItems to an image does not add exif data; the two are different tag sets!

TaW
  • 53,122
  • 8
  • 69
  • 111
  • sorry for a late answer, had to find some doc about EXIF data. Tried it on several pictures, and it works great! Thanks a lot :) – Elliott Addi Mar 23 '17 at 11:13
  • applied today. works great. just two remarks: 1) for `Rotate`, I generalized to `Image` instead of the more specific `Bitmap` - any reason not to do so? 2) the magic numbers 2-8 for the EXIF rotation value violate a coding style policy of ours, so I made an enum [just like here](http://csharphelper.com/blog/2016/07/read-an-image-files-exif-orientation-data-in-c), as they are nowhere defined/exposed in the .NET framework libraries – Cee McSharpface Aug 28 '17 at 15:08
  • 1) Well `Image` also can be Icon or WMF but I expect neither to have the orientation `PropertyItem` so I didn't want to make any promises ;-). 2) Sure, good to have such policies; I usually try to keep my answers rather bare bone, no enums, no checks etc.. Btw: you will have noticed that the `RotateFlipType` enum contains many duplicates; I have picked those I can wrap my head around most easily.. – TaW Aug 28 '17 at 15:32
  • what arguments will i supply here - PropertyItem getPropertyItemByID(Image img, int Id)? I'm confused at the Id part – paraJdox1 Mar 07 '21 at 08:07
  • Check out the whole [enumeration](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.imaging.propertyitem.id?view=net-5.0) ! – TaW Mar 07 '21 at 14:41
2

Pictures coming from a camera can contain so called EXIF metadata. This EXIF metadata can have an "orientation" tag, which many image viewing programs look at and rotate the picture accordingly when showing it. But the orientation of the image data itself remains unchanged. So in case you images come from a camera and images in landscape orientation are affected by what you describe, chances are that it is doe to the EXIF orientation tag. This is an article about this tag. Maybe there's C# code around that can help you to handle the EXIF tag, I did not check.

Heinz Kessler
  • 1,610
  • 11
  • 24