Exemple #1
0
        /// <summary>
        /// Saves the crop image.
        /// </summary>
        private void SaveCropImage()
        {
            bool isNullOfsectionValue = this.x1.Value == null &&
                                        this.x2.Value == null &&
                                        this.y1.Value == null &&
                                        this.y2.Value == null;

            if (isNullOfsectionValue)
            {
                ClientScriptHelper.ShowMessage(this.Page,
                                               "請選擇相片裁剪區域",
                                               RegisterScriptType.Start);
            }
            else
            {
                var cropUtils = new CropImageUtility(this.UploadPath, this.OriginalPath, this.CropPath);
                var result    = cropUtils.ProcessImageCrop
                                (
                    this.CurrentImage,
                    new int[]
                {
                    this.x1.Value.ConvertToInt(),
                    this.x2.Value.ConvertToInt(),
                    this.y1.Value.ConvertToInt(),
                    this.y2.Value.ConvertToInt()
                }
                                );

                if (!result["result"].Equals("Success", StringComparison.OrdinalIgnoreCase))
                {
                    ClientScriptHelper.ShowMessage(this.Page,
                                                   result["msg"],
                                                   RegisterScriptType.Start);
                }
                else
                {
                    //裁剪圖片檔名儲存到資料庫
                    _service.Update(this.ImageID, result["CropImage"]);

                    //如果有之前的裁剪圖片,則刪除
                    if (!string.IsNullOrWhiteSpace(result["OldCropImage"]))
                    {
                        cropUtils.DeleteCropImage(result["OldCropImage"]);
                    }

                    //載入裁剪圖片檔
                    LoadCropImage();

                    ClientScriptHelper.ShowMessage(this.Page,
                                                   "相片裁剪完成",
                                                   RegisterScriptType.Start);
                }
            }
        }
        public JsonResult CropImage(string id, int?x1, int?x2, int?y1, int?y2)
        {
            var result = new Dictionary <string, string>();

            if (string.IsNullOrWhiteSpace(id))
            {
                result.Add("result", "error");
                result.Add("msg", "沒有輸入資料編號");
                return(Json(result));
            }

            if (!x1.HasValue || !x2.HasValue || !y1.HasValue || !y2.HasValue)
            {
                result.Add("result", "error");
                result.Add("msg", "裁剪圖片區域值有缺少");
                return(Json(result));
            }

            Guid imageID;

            if (!Guid.TryParse(id, out imageID))
            {
                result.Add("result", "error");
                result.Add("msg", "資料編號錯誤");
                return(Json(result));
            }

            var instance = _service.FindOne(imageID);

            if (instance == null)
            {
                result.Add("result", "error");
                result.Add("msg", "資料不存在");
                return(Json(result));
            }

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

            Dictionary <string, string> processResult = cropUtils.ProcessImageCrop
                                                        (
                instance,
                new int[] { x1.Value, x2.Value, y1.Value, y2.Value }
                                                        );

            if (processResult["result"].Equals("Success", StringComparison.OrdinalIgnoreCase))
            {
                //裁剪圖片檔名儲存到資料庫
                _service.Update(instance.ID, processResult["CropImage"]);

                //如果有之前的裁剪圖片,則刪除
                if (!string.IsNullOrWhiteSpace(processResult["OldCropImage"]))
                {
                    cropUtils.DeleteCropImage(processResult["OldCropImage"]);
                }

                result.Add("result", "OK");
                result.Add("msg", "");
                result.Add("OriginalImage", string.Format(@"/{0}/{1}", this.OriginalFolder, processResult["OriginalImage"]));
                result.Add("CropImage", string.Format(@"/{0}/{1}", this.CropFolder, processResult["CropImage"]));
            }
            else
            {
                result.Add("result", processResult["result"]);
                result.Add("msg", processResult["msg"]);
            }
            return(Json(result));
        }