asp.net 圖片超過(guò)指定大小后等比例壓縮圖片的方法
/// <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
}
- 利用ASPUPLOAD,ASPJPEG實(shí)現(xiàn)圖片上傳自動(dòng)生成縮略圖及加上水印
- Asp無(wú)組件生成縮略圖的代碼
- ASP保存遠(yuǎn)程圖片到本地 同時(shí)取得第一張圖片并創(chuàng)建縮略圖的代碼
- ASP.Net 上傳圖片并生成高清晰縮略圖
- asp.net 自定義控件實(shí)現(xiàn)無(wú)刷新上傳圖片,立即顯示縮略圖,保存圖片縮略圖
- Asp.Net平臺(tái)下的圖片在線(xiàn)裁剪功能的實(shí)現(xiàn)代碼(源碼打包)
- ASP組件AspJpeg(加水印)生成縮略圖等使用方法
- ASP.NET簡(jiǎn)單好用功能齊全圖片上傳工具類(lèi)(水印、縮略圖、裁剪等)
- ASP固定比例裁剪縮略圖的方法
相關(guān)文章
Asp.Net Core輕量級(jí)Aop解決方案:AspectCore
這篇文章主要介紹了Asp.Net Core輕量級(jí)Aop解決方案:AspectCore,需要的朋友可以參考下2017-06-06排除JQuery通過(guò)HttpGet調(diào)用WebService返回Json時(shí)“parserror”錯(cuò)誤
排除JQuery通過(guò)HttpGet調(diào)用WebService返回Json時(shí)“parserror”錯(cuò)誤的解決方法。2011-10-10Asp.net調(diào)試的一些問(wèn)題小結(jié)
這篇文章主要介紹了asp.net調(diào)試方便的知識(shí)點(diǎn),在CSDN找了一些相關(guān)的解決方法,不敢獨(dú)享,供大家相互學(xué)習(xí)交流之用2013-11-11ASP.NET Core Middleware的實(shí)現(xiàn)方法詳解
中間件是組裝到應(yīng)用程序管道中以處理請(qǐng)求和響應(yīng)的軟件。下面這篇文章主要給大家介紹了關(guān)于ASP.NET Core Middleware實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2019-01-01Opencv2.4.13與Visual Studio2013環(huán)境搭建配置教程
這篇文章主要為大家詳細(xì)介紹了Opencv2.4.13 與Visual Studio2013環(huán)境搭建配置教程的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03ASP.NET MVC 從IHttp到頁(yè)面輸出的實(shí)例代碼
MVCHandler應(yīng)該算是MVC真正開(kāi)始的地方。MVCHandler實(shí)現(xiàn)了IHttpHandler接口,ProcessRequest便是方法入口2013-09-09在.NET?Core中使用CSRedis的詳細(xì)過(guò)程
這篇文章主要介紹了在.NET?Core中使用CSRedis的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06