当从iPhone等手机上传图片到服务器后,通常需要进行旋转处理,否则在进行图片压缩、缩放处理后会丢失正确的位置信息,导致显示的图片不处于正确的位置上。

处理的做法就是读取照片的Exif信息,并旋转到正确位置。代码如下:

    /// <summary>
    /// 将图片旋转到正确位置
    /// </summary>
    /// <param name="image"></param>
    /// <returns></returns>
    public static void OrientationImage(Image image)
    {
        if (Array.IndexOf(image.PropertyIdList, 274) > -1)
        {
            var orientation = (int)image.GetPropertyItem(274).Value[0];
            switch (orientation)
            {
                case 1:
                    // No rotation required.
                    break;
                case 2:
                    image.RotateFlip(RotateFlipType.RotateNoneFlipX);
                    break;
                case 3:
                    image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                    break;
                case 4:
                    image.RotateFlip(RotateFlipType.Rotate180FlipX);
                    break;
                case 5:
                    image.RotateFlip(RotateFlipType.Rotate90FlipX);
                    break;
                case 6:
                    image.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    break;
                case 7:
                    image.RotateFlip(RotateFlipType.Rotate270FlipX);
                    break;
                case 8:
                    image.RotateFlip(RotateFlipType.Rotate270FlipNone);
                    break;
            }
            image.RemovePropertyItem(274);
        }
    }

相关文章:

  • 2022-12-23
  • 2021-08-06
  • 2022-01-07
  • 2021-06-07
  • 2022-12-23
  • 2021-07-24
  • 2021-08-20
猜你喜欢
  • 2021-12-26
  • 2022-12-23
  • 2022-12-23
  • 2021-07-26
  • 2021-06-10
  • 2022-02-22
  • 2022-03-01
相关资源
相似解决方案