[C#]C# 根据图像的EXIF信息自动调整图片方向

问题场景:当我们打开图片查看图片是正常的,但是编程时候方向图片是旋转的,这个咋回事?一般我们用windows图片浏览工具或者其他看图工具都觉得是正常的,这是因为软件都经过图片旋转信息判断,都是通过图像EXIF信息进行读取判断图片是否是旋转的,一般手机拍的图片很有可能是旋转的。那么

什么是 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为基础。

        Exif 文件实际是JPEG文件的一种,遵从JPEG标准,只是在文件头信息中增加了有关拍摄信息的内容和索引图。所以你可以使用任何支持JPEG格式的图像工具软件观看或修改Exif文件,但,打开时可能看不到Exif信息,一旦修改,Exif信息可能丢失。

        所有的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


        public void GetRotateImage(Bitmap bmp)
        {
            var exif = bmp.PropertyItems;
            byte orien = 0;
            var item = exif.Where(m => m.Id == 274).ToArray();
            if (item.Length > 0)
                orien = item[0].Value[0];
            switch (orien)
            {
                case 2:
                    bmp.RotateFlip(RotateFlipType.RotateNoneFlipX);//horizontal flip
                    break;
                case 3:
                    bmp.RotateFlip(RotateFlipType.Rotate180FlipNone);//right-top
                    break;
                case 4:
                    bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);//vertical flip
                    break;
                case 5:
                    bmp.RotateFlip(RotateFlipType.Rotate90FlipX);
                    break;
                case 6:
                    bmp.RotateFlip(RotateFlipType.Rotate90FlipNone);//right-top
                    break;
                case 7:
                    bmp.RotateFlip(RotateFlipType.Rotate270FlipX);
                    break;
                case 8:
                    bmp.RotateFlip(RotateFlipType.Rotate270FlipNone);//left-bottom
                    break;
                default:
                    break;
            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FL1768317420

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值