竖向照片上传后变横向

 

 

 1. 原始图片文件 右键属性->详细信息,此宽高比应该显示为竖向,上传后却变横向。

 

 

2.查看EXIF信息图片宽高和与 1 中的宽高是颠倒的,并不相同。(在线上传图片查看EXIF:https://exif.tuchong.com/)(Exif是英文Exchangeable Image File(可交换图像文件)的缩写,相机文件设计标准,记录数码照片的属性信息和拍摄数据。

 

 

3.发现问题是 EXIF方向默认旋转90度

 

 

 

 4.需代码顺时针方向旋转90度后此方向属性变0度,保存翻转后图片即正常。

 1             string fileName = file.FileName; //file为HTTP上传文件
 2             string extname = Path.GetExtension(fileName);
 3             Stream stream = file.InputStream;
 4             System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
 5 
 6             byte orien = 0;
 7             var item = image.PropertyItems.Where(o => o.Id == 274).ToList();
 8             if (item!=null && item.Count() > 0)orien = item[0].Value[0];
 9             if(orien == 6)
10             {
11                 image.RotateFlip(RotateFlipType.Rotate90FlipNone);
12             }
13 
14             MemoryStream ms = new MemoryStream();
15             try
16             {
17                 if (extname.ToLower().Equals(".jpg"))
18                 {
19                     image.Save(ms, ImageFormat.Jpeg);
20                 }
21                 else
22                 {
23                     image.Save(ms, image.RawFormat);
24                 }
25 
26             }
27             catch
28             {
29                 image.Save(ms, ImageFormat.MemoryBmp);
30             }

 

 

5. 代码里PropertyItems的ID为什么是274,orien 为什么是6(https://docs.microsoft.com/en-us/windows/desktop/gdiplus/-gdiplus-constant-property-item-descriptions#propertytagorientationhttps://yq.aliyun.com/articles/492291)。

 

 

 

 

 

6.Exif的Orientation说明(https://www.cnblogs.com/csonezp/p/5564809.htmlhttps://blog.csdn.net/ouyangtianhan/article/details/29825885

 

 

 

posted @ 2019-05-22 13:29  ZpfCoder  阅读(6007)  评论(1编辑  收藏  举报