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

PropertyItems

代码

复制代码
        /// <summary>
        /// 根据图片exif调整方向
        /// </summary>
        /// <param name="img"></param>
        public void RotateImage(Bitmap img)
        {
            var exif = img.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:
                    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
                    break;
                case 7:
                    img.RotateFlip(RotateFlipType.Rotate270FlipX);
                    break;
                case 8:
                    img.RotateFlip(RotateFlipType.Rotate270FlipNone);//left-bottom
                    break;
                default:
                    break;
            }
        }

什么是 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

相关推荐
o0向阳而生0o3 小时前
28、.NET 中元数据是什么?
microsoft·c#·.net
niuTaylor4 小时前
Linux驱动开发快速上手指南:从理论到实战
linux·运维·开发语言·驱动开发·c#
军训猫猫头4 小时前
89.WPF 中实现便捷的数字输入框:DecimalUpDown 控件的使用 WPF例子 C#例子.
开发语言·c#·wpf
冰茶_6 小时前
.NET MAUI 发展历程:从 Xamarin 到现代跨平台应用开发框架
学习·microsoft·微软·c#·.net·xamarin
The Future is mine7 小时前
C# new Bitmap(32043, 32043, PixelFormat.Format32bppArgb)报错:参数无效,如何将图像分块化处理?
开发语言·c#
Iotfsd15 小时前
.NET写的开源工业物联网网关(IoTGateway)
物联网·c#·.net·dotnet·边缘网关·雾计算·工业物联网智能网关
先生沉默先15 小时前
c#接口_抽象类_多态学习
开发语言·学习·c#
江沉晚呤时15 小时前
深入了解C# List集合及两种常见排序算法:插入排序与堆排序
windows·sql·算法·oracle·c#·排序算法·mybatis
iReachers15 小时前
使用命令行加密混淆C#程序
开发语言·c#
[太阳]8816 小时前
Kafka命令行的使用/Spark-Streaming核心编程(二)
c#·linq