//========================================================================================= // Crop Image #region -- ProcessImageCrop -- /// <summary> /// Processes the image crop. /// </summary> public Dictionary <string, string> ProcessImageCrop( UploadImage currentImage, int[] sectionValue) { var result = new Dictionary <string, string>(); try { //取得裁剪的區域座標 var sectionX1 = sectionValue[0]; var sectionX2 = sectionValue[1]; var sectionY1 = sectionValue[2]; var 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(100, 100)); //將縮圖處理完成的圖檔儲存為JPG格式 var fileName = String.Concat(MiscUtility.MakeGuid().Replace("-", string.Empty).Substring(0, 20), ".jpg"); var 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); }