程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> asp.net 圖片超過指定大小後等比例壓縮圖片的方法

asp.net 圖片超過指定大小後等比例壓縮圖片的方法

編輯:ASP.NET基礎

復制代碼 代碼如下:
/// <summary>
        /// 壓縮圖片
        /// </summary>
        /// <returns></returns>
        public string ResizePic()
        {
            #region 壓縮圖片開始
            bool IsImgFile = true;  //判斷是否為圖片文件
            string filePathName = "123";   //文件存儲的路徑(文件夾名稱)
            string fileName = "a.jpg";   //上傳文件的原始名稱
            string fileSysName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "_" + fileName;  //修改後的文件名稱
            string filePath = "";   //文件路徑
            string strImgPath = "/fileupload/";   //上傳路徑
            if (IsImgFile)
            {
                int maxWidth = 600;   //圖片寬度最大限制
                int maxHeight = 400;  //圖片高度最大限制
                System.Drawing.Image imgPhoto =
                    System.Drawing.Image.FromFile(Server.MapPath(strImgPath) + filePathName + "/" + fileSysName);
                int imgWidth = imgPhoto.Width;
                int imgHeight = imgPhoto.Height;
                if (imgWidth > imgHeight)  //如果寬度超過高度以寬度為准來壓縮
                {
                    if (imgWidth > maxWidth)  //如果圖片寬度超過限制
                    {
                        float toImgWidth = maxWidth;   //圖片壓縮後的寬度
                        float toImgHeight = imgHeight / (float)(imgWidth / toImgWidth); //圖片壓縮後的高度

                        System.Drawing.Bitmap img = new System.Drawing.Bitmap(imgPhoto,
                                                                              int.Parse(toImgWidth.ToString()),
                                                                              int.Parse(toImgHeight.ToString()));
                        string strResizePicName = Server.MapPath(strImgPath) + filePathName + "/_small_" + fileSysName;
                        img.Save(strResizePicName);  //保存壓縮後的圖片
                        filePath = strImgPath + filePathName + "/_small_" + fileSysName;  //返回壓縮後的圖片路徑
                    }
                }
                else
                {
                    if (imgHeight > maxHeight)
                    {
                        float toImgHeight1 = maxHeight;
                        float toImgWidth1 = imgWidth / (float)(imgHeight / toImgHeight1);

                        System.Drawing.Bitmap img = new System.Drawing.Bitmap(imgPhoto,
                                                                              int.Parse(toImgWidth1.ToString()),
                                                                              int.Parse(toImgHeight1.ToString()));
                        string strResizePicName = Server.MapPath(strImgPath) + filePathName + "/_small_" + fileSysName;
                        img.Save(strResizePicName);
                        filePath = strImgPath + filePathName + "/_small_" + fileSysName;
                    }
                }
            }
            return filePath;
            #endregion
        }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved