豎向照片上傳后變橫向


 

 

 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

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM