Exemple #1
0
        /// <summary>
        /// Processes the save.
        /// </summary>
        private void ProcessSave()
        {
            if (Session["Upload_File"] != null && !string.IsNullOrWhiteSpace(Session["Upload_File"].ToString()))
            {
                var fileName = Session["Upload_File"].ToString();

                var cropUtils = new CropImageUtility(this.UploadPath, this.OriginalPath, "");
                var result    = cropUtils.SaveUploadImageToOriginalFolder(fileName);

                if (!result["result"].Equals("Success", StringComparison.OrdinalIgnoreCase))
                {
                    ClientScriptHelper.ShowMessage(this.Page, result["msg"], RegisterScriptType.Start);
                }
                else
                {
                    var instance = new UploadImage
                    {
                        ID            = Guid.NewGuid(),
                        OriginalImage = fileName,
                        CreateDate    = DateTime.Now
                    };
                    instance.UpdateDate = instance.CreateDate;

                    _service.Add(instance);

                    this.HiddenField_ID.Value = instance.ID.ToString();

                    this.Image_Upload.ImageUrl = string.Format("{0}/{1}/{2}",
                                                               this.WebSiteRootPath,
                                                               this.OriginalFolder.Replace("~", ""),
                                                               fileName);

                    this.Button_Save.Visible   = false;
                    this.Button_Cancel.Visible = false;

                    if (!string.IsNullOrWhiteSpace(this.HiddenField_ID.Value))
                    {
                        this.Button_Crop.Visible = true;
                    }
                }
            }
        }
        public JsonResult Save(string fileName)
        {
            var jo = new Dictionary <string, string>();

            var cropUtils = new CropImageUtility(this.UploadPath, this.OriginalPath, "");

            var result = cropUtils.SaveUploadImageToOriginalFolder(fileName);

            if (!result["result"].Equals("Success", StringComparison.OrdinalIgnoreCase))
            {
                jo.Add("result", result["result"]);
                jo.Add("msg", result["msg"]);
                return(Json(jo));
            }

            try
            {
                var instance = new ImageCrop.Common.UploadImage
                {
                    ID            = Guid.NewGuid(),
                    OriginalImage = fileName,
                    CreateDate    = DateTime.Now
                };
                instance.UpdateDate = instance.CreateDate;

                _service.Add(instance);

                jo.Add("result", "Success");
                jo.Add("msg", string.Format(@"/{0}/{1}", OriginalFolder, fileName));
                jo.Add("id", instance.ID.ToString());
            }
            catch (Exception ex)
            {
                jo.Add("result", "Exception");
                jo.Add("msg", ex.Message);
            }
            return(Json(jo));
        }
        //=========================================================================================
        // 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);
        }