asp.net 圖片超過(guò)指定大小后等比例壓縮圖片的方法_.Net教程
推薦:CheckBoxList兩列并排編譯為表格顯示具體實(shí)現(xiàn)CheckBoxList兩列并排的顯示效果相比大家都有見(jiàn)到過(guò)吧,下面是具體的實(shí)現(xiàn)代碼,感興趣的朋友可以參考下哈
/// <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
}
分享:GridView中動(dòng)態(tài)設(shè)置CommandField是否可用或可見(jiàn)的小例子GridView中動(dòng)態(tài)設(shè)置CommandField是否可用或可見(jiàn)的小例子,需要的朋友可以參考一下
- asp.net如何得到GRIDVIEW中某行某列值的方法
- .net SMTP發(fā)送Email實(shí)例(可帶附件)
- js實(shí)現(xiàn)廣告漂浮效果的小例子
- asp.net Repeater 數(shù)據(jù)綁定的具體實(shí)現(xiàn)
- Asp.Net 無(wú)刷新文件上傳并顯示進(jìn)度條的實(shí)現(xiàn)方法及思路
- Asp.net獲取客戶(hù)端IP常見(jiàn)代碼存在的偽造IP問(wèn)題探討
- VS2010 水晶報(bào)表的使用方法
- ASP.NET中操作SQL數(shù)據(jù)庫(kù)(連接字符串的配置及獲取)
- asp.net頁(yè)面?zhèn)髦禍y(cè)試實(shí)例代碼
- DataGridView - DataGridViewCheckBoxCell的使用介紹
- asp.net中javascript的引用(直接引入和間接引入)
- 三層+存儲(chǔ)過(guò)程實(shí)現(xiàn)分頁(yè)示例代碼
.Net教程Rss訂閱編程教程搜索
.Net教程推薦
- ASP.NET 2.0當(dāng)中的Call Back機(jī)制
- 如何使用ASP.NET Image Generation生成圖片縮略圖及水印
- ASP.NET 2.0中文驗(yàn)證碼的實(shí)現(xiàn)
- 探討.Net整合其他平臺(tái)
- 解析在ASP.NET中調(diào)用存儲(chǔ)過(guò)程的方法
- 用Java發(fā)送圖文并茂的HTML郵件
- 深入Lumisoft.NET實(shí)現(xiàn)郵件發(fā)送功能的方法詳解
- Asp.net中DataTable導(dǎo)出到Excel的方法介紹
- 淺析asp.net 里 include UTF8 垃圾問(wèn)題
- Linkbutton控件在項(xiàng)目中的簡(jiǎn)單應(yīng)用
- 相關(guān)鏈接:
- 教程說(shuō)明:
.Net教程-asp.net 圖片超過(guò)指定大小后等比例壓縮圖片的方法
。