主頁 > 知識(shí)庫 > asp.net 圖片超過指定大小后等比例壓縮圖片的方法

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

熱門標(biāo)簽:Mysql連接數(shù)設(shè)置 電子圍欄 阿里云 銀行業(yè)務(wù) 科大訊飛語音識(shí)別系統(tǒng) 團(tuán)購網(wǎng)站 服務(wù)器配置 Linux服務(wù)器

復(fù)制代碼 代碼如下:

/// summary>
        /// 壓縮圖片
        /// /summary>
        /// returns>/returns>
        public string ResizePic()
        {
            #region 壓縮圖片開始
            bool IsImgFile = true;  //判斷是否為圖片文件
            string filePathName = "123";   //文件存儲(chǔ)的路徑(文件夾名稱)
            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)  //如果寬度超過高度以寬度為準(zhǔn)來壓縮
                {
                    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
        }

您可能感興趣的文章:
  • 利用ASPUPLOAD,ASPJPEG實(shí)現(xiàn)圖片上傳自動(dòng)生成縮略圖及加上水印
  • Asp無組件生成縮略圖的代碼
  • ASP保存遠(yuǎn)程圖片到本地 同時(shí)取得第一張圖片并創(chuàng)建縮略圖的代碼
  • ASP.Net 上傳圖片并生成高清晰縮略圖
  • asp.net 自定義控件實(shí)現(xiàn)無刷新上傳圖片,立即顯示縮略圖,保存圖片縮略圖
  • Asp.Net平臺(tái)下的圖片在線裁剪功能的實(shí)現(xiàn)代碼(源碼打包)
  • ASP組件AspJpeg(加水印)生成縮略圖等使用方法
  • ASP.NET簡單好用功能齊全圖片上傳工具類(水印、縮略圖、裁剪等)
  • ASP固定比例裁剪縮略圖的方法

標(biāo)簽:大理 廣元 棗莊 衢州 江蘇 萍鄉(xiāng) 蚌埠 衡水

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《asp.net 圖片超過指定大小后等比例壓縮圖片的方法》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266