2015-11-02 68 views
0

我想在上傳文件時調整圖像大小(創建縮略圖)。但似乎無法正常讀取路徑,它的崩潰......在文件上傳時調整圖像大小 - C#

CODE:

if (FileUpload1.HasFile) 
     { 
      string imageFile = FileUpload1.FileName; 
      string path = "~/images/galeria/" + imageFile; 
      cmd.Parameters.Add("@IMAGE_URL", System.Data.SqlDbType.NVarChar).Value = path; 
      FileUpload1.SaveAs(Server.MapPath(path)); 

      System.Drawing.Image image = System.Drawing.Image.FromFile(path); 
      float aspectRatio = (float)image.Size.Width/(float)image.Size.Height; 
      int newHeight = 200; 
      int newWidth = Convert.ToInt32(aspectRatio * newHeight); 
      System.Drawing.Bitmap thumbBitmap = new System.Drawing.Bitmap(newWidth, newHeight); 
      System.Drawing.Graphics thumbGraph = System.Drawing.Graphics.FromImage(thumbBitmap); 
      thumbGraph.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; 
      thumbGraph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 
      thumbGraph.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
      var imageRectangle = new Rectangle(0, 0, newWidth, newHeight); 
      thumbGraph.DrawImage(image, imageRectangle); 
      thumbBitmap.Save("~/images/galeria/thumb/" + FileUpload1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg); 
      thumbGraph.Dispose(); 
      thumbBitmap.Dispose(); 
      image.Dispose(); 
     } 

它保存圖像到目錄中,但它不會讀取路徑,這樣的aspectRatio不能得到它的大小。有任何想法嗎?

EDIT1: 錯誤消息: 一種類型「System.IO.FileNotFoundException」的異常出現在System.Drawing.dll程序但在用戶代碼中沒有處理

其他信息:這裏的ImagePath。

EDIT2: System.Drawing.dll中發生類型爲「System.Runtime.InteropServices.ExternalException」的異常,但未在用戶代碼中處理其他信息:GDI +中發生了一般性錯誤。讀取路徑

thumbBitmap.Save("~/images/galeria/thumb/" + FileUpload1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg); 
+0

您是否檢查過以確保該文件在您保存後確實存在? –

+0

是的,它被正確保存。也許它正在進行處理,在完成保存之前調整圖像大小。我該如何給它一段時間來拯救? – Ashiv3r

+0

我只是通過代碼進行調試,執行保存後,但在從文件加載之前,請檢查文件,然後運行加載並查看是否有錯誤。如果文件存在並且仍然存在錯誤,那麼這肯定不是問題。 –

回答

2

用途使用Server.Mappath(路徑): 此行引起錯誤。您正在使用它來保存,但不是用於閱讀。

if (FileUpload1.HasFile) 
     { 
      string imageFile = FileUpload1.FileName; 
      string path = Server.MapPath("~/images/galeria/" + imageFile); 
      FileUpload1.SaveAs(path); 

      System.Drawing.Image image = System.Drawing.Image.FromFile(path); 
      float aspectRatio = (float)image.Size.Width/(float)image.Size.Height; 
      int newHeight = 200; 
      int newWidth = Convert.ToInt32(aspectRatio * newHeight); 
      System.Drawing.Bitmap thumbBitmap = new System.Drawing.Bitmap(newWidth, newHeight); 
      System.Drawing.Graphics thumbGraph = System.Drawing.Graphics.FromImage(thumbBitmap); 
      thumbGraph.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; 
      thumbGraph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 
      thumbGraph.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
      var imageRectangle = new Rectangle(0, 0, newWidth, newHeight); 
      thumbGraph.DrawImage(image, imageRectangle); 
      thumbBitmap.Save(Server.MapPath("~/images/galeria/thumb/" + FileUpload1.FileName), System.Drawing.Imaging.ImageFormat.Jpeg); 
      thumbGraph.Dispose(); 
      thumbBitmap.Dispose(); 
      image.Dispose(); 
     } 
+0

它給我一個像編輯1中的錯誤。 – Ashiv3r

+0

檢查編輯。請創建文件夾結構爲images/galeria/thumb。如果文件夾結構不存在,則會引發錯誤。我運行了代碼,它很好。您也可以實用地創建這些文件夾。豎起大拇指,如果我能夠幫助:) – Thinkbeforeyouread

+0

在這一點上它仍然給我同樣的錯誤。 – Ashiv3r