這篇文章將為大家詳細講解有關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 toImgWidth2 = imgWidth / (float)(imgHeight / toImgHeight1);
System.Drawing.Bitmap img = new System.Drawing.Bitmap(imgPhoto,
int.Parse(toImgWidth2.ToString()),
int.Parse(toImgHeight1.ToString()));
string strResizePicName = Server.MapPath(strImgPath) + filePathName + "/_small_" + fileSysName;
img.Save(strResizePicName);
filePath = strImgPath + filePathName + "/_small_" + fileSysName;
}
}
}
return filePath;
#endregion
}
關于“asp.net如何實現圖片超過指定大小后等比例壓縮圖片功能”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
文章標題:asp.net如何實現圖片超過指定大小后等比例壓縮圖片功能-創新互聯
網頁路徑:http://newbst.com/article10/dgghgo.html
成都網站建設公司_創新互聯,為您提供商城網站、網頁設計公司、外貿建站、App設計、自適應網站、標簽優化
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯