2017-04-19 131 views
3

我要上傳圖片上傳圖片顯示出來側身

<input class="images" type="file" id="item" name="Images" /> 

它,然後被保存到我的項目,這樣

Guid g = Guid.NewGuid(); 
       Images.SaveAs(Server.MapPath("~/Uploads/" + g + ".jpg")); 
       fileNames = g.ToString() + ".jpg"; 

由於某種原因選擇有人上傳圖片時從移動到網站它顯示橫向?

+1

道歉看看這個答案的EXIF方向:HTTP:/ /stackoverflow.com/questions/27835064/get-image-orientation-and-rotate-as-per-orientation – nixkuroi

+1

您需要檢查圖像元數據(exif),然後根據八個方向之一進行旋轉。這可以在上傳之前或在服務器上通過JavaScript完成。 – Jasen

回答

2

您可以查看文件的元數據以查看它旋轉的方式。

具體而言,將圖像拉入.NET圖像類型,然後調用img.GetPropertyItem(& H112).Value(0)。

這將返回一個整數,它表示圖像的「旋轉值」。

1 = Landscape 
3 = Upside-down 
6 = Rotated 90 degrees left 
8 = Rotated 90 degrees right 

一旦你知道了,你可以使用img.RotateFlip方法旋轉圖像。

下面是我寫的解決非常類似問題的課。

相關代碼位於RotateImage方法中。

注:這是在VB.NET,我跑它通過Telerik的代碼轉換器,所以我對任何奇怪的語法

//get the image from the file they gave us, resize it, and rotate it if needed 
    OnlineImage onlineImageHelper = new OnlineImage(Context.Request.Files(0).InputStream); 
    byte[] pictureLarger = onlineImageHelper.StraightenedThumbnail(new Size(180, 180)); 
    byte[] pictureSmaller = onlineImageHelper.StraightenedThumbnail(new Size(80, 80)); 

using Microsoft.VisualBasic; 
using System; 
using System.Collections; 
using System.Collections.Generic; 
using System.Data; 
using System.Diagnostics; 
using System.Drawing; 
using System.Drawing.Drawing2D; 
using System.Drawing.Imaging; 
using System.IO; 

public class OnlineImage 
{ 
    public OnlineImage() 
    { 
     throw new NotImplementedException(); 
    } 

    public OnlineImage(Stream imageStream) 
    { 
     _ImageFromUser = Image.FromStream(imageStream); 
     RotateImage(); 
    } 

    private Image _ImageFromUser; 
    private Image _RotatedImage; 
    private Image _ResizedAndRotatedImage; 

    private void RotateImage() 
    { 
     if (_RotatedImage == null && _ImageFromUser != null && _ImageFromUser.PropertyIdList != null && _ImageFromUser.PropertyIdList.Contains(0x112)) { 
      int rotationValue = _ImageFromUser.GetPropertyItem(0x112).Value(0); 
      switch (rotationValue) { 
       case 1: 
        // landscape, do nothing 
        break; 
       case 8: 
        // rotated 90 right 
        // de-rotate: 
        _ImageFromUser.RotateFlip(rotateFlipType: RotateFlipType.Rotate270FlipNone); 
        break; 
       case 3: 
        // bottoms up 
        _ImageFromUser.RotateFlip(rotateFlipType: RotateFlipType.Rotate180FlipNone); 
        break; 
       case 6: 
        // rotated 90 left 
        _ImageFromUser.RotateFlip(rotateFlipType: RotateFlipType.Rotate90FlipNone); 
        break; 
      } 
      _RotatedImage = _ImageFromUser; 
     } 
    } 

    private void ResizeImage(Size size, bool preserveAspectRatio = true) 
    { 
     int newWidth = 0; 
     int newHeight = 0; 
     if (preserveAspectRatio) { 
      int originalWidth = _ImageFromUser.Width; 
      int originalHeight = _ImageFromUser.Height; 
      float percentWidth = Convert.ToSingle(size.Width)/Convert.ToSingle(originalWidth); 
      float percentHeight = Convert.ToSingle(size.Height)/Convert.ToSingle(originalHeight); 
      float percent = percentHeight < percentWidth ? percentHeight : percentWidth; 
      newWidth = Convert.ToInt32(originalWidth * percent); 
      newHeight = Convert.ToInt32(originalHeight * percent); 
     } else { 
      newWidth = size.Width; 
      newHeight = size.Height; 
     } 

     _ResizedAndRotatedImage = new Bitmap(newWidth, newHeight); 

     using (Graphics graphicsHandle = Graphics.FromImage(_ResizedAndRotatedImage)) { 
      graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic; 
      graphicsHandle.DrawImage(_ImageFromUser, 0, 0, newWidth, newHeight); 
     } 
    } 

    public byte[] StraightenedThumbnail(Size resizedDimensions) 
    { 
     byte[] result = null; 
     MemoryStream msPicture = new MemoryStream(); 
     ResizeImage(resizedDimensions); 
     if (_ResizedAndRotatedImage != null) { 
      _ResizedAndRotatedImage.Save(msPicture, ImageFormat.Png); 
      result = msPicture.ToArray(); 
      return result; 
     } 

     return null; 
    } 
} 
+0

圖像是一個HttpPostedFileBase是不是一個.NET圖像類型?爲什麼它不知道.GetPropertyItem是什麼? – Newbie

+0

當我說「.net圖像類型」,我指的是「System.Drawing.Image」 – JosephStyons

+0

嗯我想要的類型。不應該這樣工作嗎? Images.SaveAs(Server.MapPath(「〜/ Uploads /」+ g +「.jpg」)); fileNames = g.ToString()+「.jpg」; Image img = Image.FromFile(fileNames);我究竟做錯了什麼? – Newbie