C#实现根据图片的EXIF自动调整图片方向

10 篇文章 0 订阅

一、什么是 EXIF

Exif是英文Exchangeable Image File(可交换图像文件)的缩写,最初由日本电子工业发展协会(JEIDA --Japan Electronic Industry Development Association) 制订,目前的最新版本是发表于2002年04月的2.21 版。国际标准化组织(ISO)正在制订的相机文件设计标准(DCF -- Design role for Camera File system)可能以Exif2.1为基础。

所有的JPEG文件以字符串“0xFFD8”开头,并以字符串“0xFFD9”结束。文件头中有一系列“0xFF??”格式的字符串,称为“标识”,用来标记JPEG文件的信息段。“0xFFD8”表示图像信息开始,“0xFFD9”表示图像信息结束,这两个标识后面没有信息,而其它标识紧跟一些信息字符。

0xFFE0 -- 0xFFEF之间的标识符称为“应用标记”,没有被常规JPEG文件利用,Exif正是利用这些信息串记录拍摄信息如快门速度、光圈值等,甚至可以包括全球定位信息。其中拍摄方向的ID为“0x0112”,有1至8共8种值。
在这里插入图片描述

二、EXIF Orientation

Orientation
The image orientation viewed in terms of rows and columns.
按行和列查看的图像方向

Tag = 274 (112.H)

Type = SHORT
Count = 1
Default = 1

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

三、使用C#旋转图片

public static void rotating(Bitmap img, ref int width, ref int height, int orien)
{
    int ow = width;
    switch(orien)
    {
        case 2:
            img.RotateFlip(RotateFlipType.RotateNoneFlipX); //horizontal flip
            break;
        case 3:
            img.RotateFlip(RotateFlipType.Rotate180FlipNone); //right-top
            break;
        case 4:
            img.RotateFlip(RotateFlipType.RotateNoneFlipY); //vertical flip
            break;
        case 5:
            img.RotateFlip(RotateFlipType.Rotate90FlipX);
            break;
        case 6:
            img.RotateFlip(RotateFlipType.Rotate90FlipNone); //right-top
            width = height;
            height = ow;
            break;
        case 7:
            img.RotateFlip(RotateFlipType.Rotate270FlipX);
            break;
        case 8:
            img.RotateFlip(RotateFlipType.Rotate270FlipNone); //left-bottom
            width = height;
            height = ow;
            break;
        default:
            break;
    }
}

.Net Core 通过Exif处理手机图片旋转问题

获取图片 exif 的 orientation 信息核心代码,然后根据 orientation 进行图片不同方向的旋转

效果展示

在这里插入图片描述

核心代码

private Image 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);

    }
    return image;
}

文件转为image类型

上传的图片文件(file(IFormFile) 或者 base64(string) 需转为 image,然后直接调用上面方法,输出新的 image
保存(save方法)即可

file 的话

 Image img = Bitmap.FromStream(file.OpenReadStream()); 

base64 的话

 string base64string = HttpUtility.UrlDecode(base64);
 base64string = base64string.Replace(" ", "+").Split(',')[1];
 byte[] bt = Convert.FromBase64String(base64string);
 MemoryStream ms = new MemoryStream(bt);
 Image img = Image.FromStream(ms);

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值