Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

c# - Get Image Orientation and rotate as per orientation

having problem in getting image orientation with below code

    string fileName = @"D:......1012015004435.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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...