国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > .NET > 正文

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

2024-07-10 13:16:24
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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


/// <summary>
        /// 壓縮圖片
        /// </summary>
        /// <returns></returns>
        public string ResizePic()
        {
            #region 壓縮圖片開(kāi)始
            bool IsImgFile = true;  //判斷是否為圖片文件
            string filePathName = "123";   //文件存儲(chǔ)的路徑(文件夾名稱(chēng))
            string fileName = "a.jpg";   //上傳文件的原始名稱(chēng)
            string fileSysName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "_" + fileName;  //修改后的文件名稱(chēng)
            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)  //如果寬度超過(guò)高度以寬度為準(zhǔn)來(lái)壓縮
                {
                    if (imgWidth > maxWidth)  //如果圖片寬度超過(guò)限制
                    {
                        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
        }

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 家居| 泸溪县| 安庆市| 乡宁县| 永年县| 微山县| 南康市| 进贤县| 双辽市| 石景山区| 灵丘县| 海淀区| 柳江县| 登封市| 察雅县| 天气| 武城县| 庆元县| 石台县| 大方县| 资兴市| 新竹市| 永嘉县| 高邑县| 南平市| 昆山市| 阳泉市| 县级市| 龙井市| 无极县| 凤阳县| 陆川县| 阜新| 长丰县| 翼城县| 常熟市| 新平| 土默特左旗| 阳东县| 都安| 仪征市|