Ejemplo n.º 1
0
 /// <summary>
 /// 转换图片格式
 /// </summary>
 /// <param name="oldPath">原图片路径</param>
 /// <param name="newPath">新图片路径</param>
 public static bool changeFormat(string oldPath, string newPath)
 {
     try
     {
         //** psd convert to jpg 、gift、png
         MagickNet.Magick.Init();
         //** find old path
         //** eg:"~/Images/yz1309.psd"
         MagickNet.Image img = new MagickNet.Image(HttpContext.Current.Server.MapPath(oldPath));
         //** set new image size
         System.Drawing.Size size = new System.Drawing.Size(img.BaseColumns.ToInt() / 10, img.BaseRows.ToInt() / 10);
         img.Resize(size);
         string path = newPath.Substring(0, newPath.LastIndexOf("/"));
         //** create directory if not exist
         string savePath = HttpContext.Current.Server.MapPath(path);
         if (!Directory.Exists(savePath))
         {
             Directory.CreateDirectory(savePath);
         }
         //** save new file
         //** eg:"~/Images/yz1309.jpg"
         img.Write(HttpContext.Current.Server.MapPath(newPath));
         MagickNet.Magick.Term();
         return(true);
     }
     catch { return(false); }
 }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
0
        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();
        }