我想在上傳文件時調整圖像大小(創建縮略圖)。但似乎無法正常讀取路徑,它的崩潰......在文件上傳時調整圖像大小 - 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);
您是否檢查過以確保該文件在您保存後確實存在? –
是的,它被正確保存。也許它正在進行處理,在完成保存之前調整圖像大小。我該如何給它一段時間來拯救? – Ashiv3r
我只是通過代碼進行調試,執行保存後,但在從文件加載之前,請檢查文件,然後運行加載並查看是否有錯誤。如果文件存在並且仍然存在錯誤,那麼這肯定不是問題。 –