/// <summary> /// Makes the thumbnail image. /// </summary> /// <param name="fileName">Name of the file.</param> /// <returns></returns> private string MakeThumbnailImage(string fileName) { //如果上傳圖片寬度大於 maxWidth,確定儲存上傳圖片就要做縮圖動作 string tempFileName = fileName; int uploadImageWidth = 0; int uploadImageHeight = 0; using (Bitmap bitmap = new Bitmap(string.Format(@"{0}\{1}", this.UploadPath, tempFileName))) { uploadImageWidth = bitmap.Width; uploadImageHeight = bitmap.Height; if (uploadImageWidth > this.MaxWidth || uploadImageHeight > this.MaxHeight) { // 計算維持比例的縮圖大小 int[] thumbnailScale = this.GetThumbnailImageScale(this.MaxWidth, this.MaxHeight, uploadImageWidth, uploadImageHeight); // 產生縮圖 System.Drawing.Image imgThumbnail = System.Drawing.Image.FromFile(string.Format(@"{0}\{1}", this.UploadPath, tempFileName), false); System.Drawing.Image hb = new System.Drawing.Bitmap(thumbnailScale[0], thumbnailScale[1]); System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(hb); graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; graphics.DrawImage(imgThumbnail, new Rectangle(0, 0, thumbnailScale[0], thumbnailScale[1]), 0, 0, imgThumbnail.Width, imgThumbnail.Height, GraphicsUnit.Pixel); //存檔 fileName = String.Concat(MiscUtility.MakeGuid().Replace("-", string.Empty).Substring(0, 20), ".jpg"); hb.Save(string.Format(@"{0}\{1}", this.UploadPath.Replace("~", ""), fileName)); graphics.Dispose(); hb.Dispose(); imgThumbnail.Dispose(); } } return(fileName); }
//========================================================================================= // Upload Image #region -- ProcessUploadImage -- /// <summary> /// Processes the upload image. /// </summary> /// <param name="uploadFile">The upload file.</param> /// <returns></returns> public Dictionary <string, string> ProcessUploadImage(HttpPostedFileBase uploadFile) { var jo = new Dictionary <string, string>(); if (uploadFile.ContentLength <= 0) { jo.Add("result", "error"); jo.Add("msg", "無內容檔案!請重新選擇檔案!"); return(jo); } else if (uploadFile.ContentLength >= MaxRequestLength) { //容量超過指定的上限, 預設 10M (10240KB) string limitLength = string.Empty; if (this.MaxRequestLength >= (1024 * 1024)) { limitLength = string.Concat((this.MaxRequestLength / (1024 * 1024)).ToString(), " Mb"); } else if ((this.MaxRequestLength / (1024 * 1024)) < 1) { limitLength = string.Concat((this.MaxRequestLength / 1024).ToString(), " Kb"); } jo.Add("result", "error"); jo.Add("msg", string.Format("上傳檔案不可超過 {0}!請重新選擇檔案!", limitLength)); return(jo); } else { if (!IsImage(uploadFile)) { jo.Add("result", "error"); jo.Add("msg", "上傳檔案並不是圖片檔!請重新選擇檔案!"); return(jo); } // Picture Image_Type var imageTypes = new string[] { "jpg", "jpeg", "png", "gif" }; if (!imageTypes.Contains(Path.GetExtension(uploadFile.FileName).Substring(1, 3).ToLower())) { jo.Add("result", "error"); jo.Add("msg", "上傳檔案的圖片檔只能接受 jpg, jpeg, png, gif!請重新選擇檔案!"); return(jo); } try { //存檔 string fileName = String.Concat(MiscUtility.MakeGuid().Replace("-", string.Empty).Substring(0, 20), Path.GetExtension(uploadFile.FileName).ToLower()); uploadFile.SaveAs(string.Format(@"{0}\{1}", this.UploadPath, fileName)); jo.Add("result", "Success"); jo.Add("msg", fileName); } catch (Exception ex) { jo.Add("result", "Failure"); jo.Add("msg", ex.Message); } return(jo); } }
//========================================================================================= // Crop Image #region -- ProcessImageCrop -- /// <summary> /// Processes the image crop. /// </summary> /// <param name="currentImage">The current image.</param> /// <param name="sectionValue">The section value.</param> /// <returns></returns> public Dictionary <string, string> ProcessImageCrop( UploadImage currentImage, int[] sectionValue) { var result = new Dictionary <string, string>(); try { //取得裁剪的區域座標 int sectionX1 = sectionValue[0]; int sectionX2 = sectionValue[1]; int sectionY1 = sectionValue[2]; int sectionY2 = sectionValue[3]; //取得裁剪的圖片寬高 int width = sectionX2 - sectionX1; int height = sectionY2 - sectionY1; //讀取原圖片 System.Drawing.Image sourceImage = System.Drawing.Image.FromFile ( string.Format(@"{0}\{1}", this.OriginalPath, currentImage.OriginalImage) ); //從原檔案取得裁剪圖片 System.Drawing.Image cropImage = this.CropImage( sourceImage, new Rectangle(sectionX1, sectionY1, width, height) ); //將採剪下來的圖片做縮圖處理 Bitmap resizeImage = this.ResizeImage(cropImage, new Size(this.CropWidth, this.CropHeight)); //將縮圖處理完成的圖檔儲存為JPG格式 string fileName = String.Concat(MiscUtility.MakeGuid().Replace("-", string.Empty).Substring(0, 20), ".jpg"); string savePath = string.Format(@"{0}\{1}", this.CropPath, fileName); SaveJpeg(savePath, resizeImage, 100L); //釋放檔案資源 resizeImage.Dispose(); cropImage.Dispose(); sourceImage.Dispose(); //如果有之前的裁剪圖片,暫存既有的裁剪圖片檔名 string oldCropImageFileName = string.Empty; if (!string.IsNullOrWhiteSpace(currentImage.CropImage)) { oldCropImageFileName = currentImage.CropImage; } //JSON result.Add("result", "Success"); result.Add("OriginalImage", currentImage.OriginalImage); result.Add("CropImage", fileName); result.Add("OldCropImage", oldCropImageFileName); } catch (Exception ex) { result.Add("result", "Exception"); result.Add("msg", ex.Message); } return(result); }