Get Image Orientation and rotate as per orientation

前端 未结 3 1560
后悔当初
后悔当初 2020-11-27 16:29

having problem in getting image orientation with below code

    string fileName = @\"D:\\...\\...\\01012015004435.jpeg\";

    int rotate = 0;
    using (var         


        
3条回答
  •  广开言路
    2020-11-27 17:06

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

提交回复
热议问题