/// <summary> /// 儲存放縮圖片 /// </summary> /// <param name="imagePath">圖片路徑</param> /// <param name="saveToPath">放縮儲存圖片路徑</param> /// <param name="width">寬度</param> /// <param name="height">高度</param> /// <returns>bool:是否儲存成功</returns> public static bool saveThumbnail(string imagePath, string saveToPath, int width, int height) { //初始化 bool recode = false; try { //原圖片是否存在 if (!System.IO.File.Exists(imagePath)) { throw new Exception("[" + imagePath + "]該圖片路徑不存在!"); } //引用Image MagickNet.Image img = new MagickNet.Image(imagePath); img.Quality = 100; //引用Bitmap System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(imagePath); //原寬 int sourceWidth = bitmap.Width; //原高 int sourceHeight = bitmap.Height; // int widthRate = width, heightRate = height; //寬比例 if (width < 1 && height > 0) { //寬比例 widthRate = Convert.ToInt32((sourceWidth * height) / sourceHeight); } //高比例 else if (height < 1 && width > 0) { //高比例 heightRate = Convert.ToInt32((sourceHeight * width) / sourceWidth); } //比例圖片 img.Resize(new System.Drawing.Size(widthRate, heightRate)); //儲存 img.Write(saveToPath); //關閉img img.Dispose(); recode = true; } //拋出例外 catch (Exception e) { log.Error("MagickScale.saveThumbnail error:" + e.Message); } //操作訊息 return(recode); }
public void ProcessRequest(HttpContext context) { string funcName = context.Request.QueryString["funcName"]; string resultMsg = ""; #region MyRegion Dictionary <string, string> resultDic = new Dictionary <string, string>(); resultDic["result"] = "false"; resultDic["message"] = ""; string fileRootPath = ""; switch (funcName) { case "userImg": { fileRootPath = Eva.Library.Configuration.ConfigurationManager.AppSettings["UserPhotoUrlRootPath"].ToString(); } break; case "businessImg": { fileRootPath = Eva.Library.Configuration.ConfigurationManager.AppSettings["FileUpLoadRootPath"].ToString(); } break; } string uploadPath = fileRootPath + "fileuploadpath/"; string uploadName = Eva.Library.Text.NumberTool.GetNoRepeatNumber() + ".png"; string uploadFullPath = Eva.Library.Format.FormatTextTool.GetMapPath(uploadPath + uploadName, HttpContext.Current.Server); resultDic = upLoadFile(uploadPath, uploadName, context); if (resultDic["result"] == "true") { #region 压缩 string thumbnailPath = fileRootPath + "thumbnailpath/"; string thumbnailName = sara.dd.ldsw.commonclass.commonclass.makeThumbnailName(uploadName); string thumbnailFullPath = Eva.Library.Format.FormatTextTool.GetMapPath(thumbnailPath + thumbnailName, HttpContext.Current.Server); MagickNet.Image img = new MagickNet.Image(uploadFullPath); if (img.Orientation == MagickNet.OrientationType.RightTopOrientation) { img.Rotate(90); img.Orientation = MagickNet.OrientationType.UndefinedOrientation; } else if (img.Orientation == MagickNet.OrientationType.BottomRightOrientation) { img.Rotate(180); img.Orientation = MagickNet.OrientationType.UndefinedOrientation; } else if (img.Orientation == MagickNet.OrientationType.LeftBottomOrientation) { img.Rotate(270); img.Orientation = MagickNet.OrientationType.UndefinedOrientation; } //图片质量 img.Quality = 100; //調整大小,是等比的,就是说宽长压宽,高长压高 img.Resize(new MagickNet.Geometry(120, 120)); img.Write(thumbnailFullPath); img.Dispose(); #endregion } resultMsg = Eva.Library.Format.FormatEntityTool.FormatDicToJson(resultDic); #endregion context.Response.Output.Write(resultMsg); context.Response.Output.Flush(); }