.Net 實現(xiàn)圖片縮略圖上傳通用方法
日常開發(fā)中,經(jīng)常碰到圖片上傳的需求,尤其在商城系統(tǒng)開發(fā)的時候,商品列表商品圖片展示如果使用高清原圖,由于高清原圖比較大,加載原圖時間會大大增加,直接導致系統(tǒng)性能底下,用戶體驗不好,并發(fā)量高的時候直接就掛掉了,這時候后臺上傳圖片的時候,就必須將原高清圖進行壓縮,生成高質(zhì)量縮略圖,然后在商品列表讀取縮略圖可以大大減少加載時間,起到一個性能優(yōu)化的作用,當然在商品詳情的時候還是得用高清原圖!
以下代碼,可以在實際開發(fā)中使用將圖片高質(zhì)量壓縮,話不多說,代碼貼下:
/// <summary>
/// 生成縮略圖或質(zhì)量壓縮
/// </summary>
/// <param name="sourcePath">源圖路徑(物理路徑)</param>
/// <param name="targetPath">縮略圖路徑(物理路徑)</param>
/// <param name="width">縮略圖寬度,如果寬度為0則不縮略</param>
/// <param name="height">縮略圖高度,如果高度為0則不縮略</param>
/// <param name="mode">生成縮略圖的方式,默認為空,為空則不縮略高寬[HW 指定高寬縮放(不變形);W 指定寬,高按比例;H 指定高,寬按比例;CUT 指定高寬裁減(不變形)]</param>
/// <param name="flag">壓縮質(zhì)量(數(shù)字越小壓縮率越高)1-100</param>
/// <param name="size">壓縮后圖片的最大大小,0為不限制大小</param>
public static void MakeThumbnail(string sourcePath, string targetPath, int width = 0, int height = 0, string mode = "", int flag = 100, int size = 0)
{
Image sourceImage = null;
Image bitmap = null;
Graphics g = null;
EncoderParameters ep = null;
EncoderParameter eParam = null;
try
{
sourceImage = Image.FromFile(sourcePath);
int toWidth = 0;
if (width > 0)
{
toWidth = width;
}
else
{
toWidth = sourceImage.Width;
}
int toHeight = 0;
if (height > 0)
{
toHeight = height;
}
else
{
toHeight = sourceImage.Height;
}
int x = 0;
int y = 0;
int ow = sourceImage.Width;
int oh = sourceImage.Height;
if (width > 0 && height > 0 && !string.IsNullOrWhiteSpace(mode))
{
switch (mode.ToUpper())
{
case "HW"://指定高寬縮放(不變形)
int tempheight = sourceImage.Height * width / sourceImage.Width;
if (tempheight > height)
{
toWidth = sourceImage.Width * height / sourceImage.Height;
}
else
{
toHeight = sourceImage.Height * width / sourceImage.Width;
}
break;
case "W"://指定寬,高按比例
toHeight = sourceImage.Height * width / sourceImage.Width;
break;
case "H"://指定高,寬按比例
toWidth = sourceImage.Width * height / sourceImage.Height;
break;
case "CUT"://指定高寬裁減(不變形)
if ((double)sourceImage.Width / (double)sourceImage.Height > (double)toWidth / (double)toHeight)
{
oh = sourceImage.Height;
ow = sourceImage.Height * toWidth / toHeight;
y = 0;
x = (sourceImage.Width - ow) / 2;
}
else
{
ow = sourceImage.Width;
oh = sourceImage.Width * height / toWidth;
x = 0;
y = (sourceImage.Height - oh) / 2;
}
break;
}
}
//新建一個bmp圖片
bitmap = new Bitmap(toWidth, toHeight);
//新建一個畫板
g = Graphics.FromImage(bitmap);
g.CompositingQuality = CompositingQuality.HighQuality;
//設(shè)置高質(zhì)量插值法
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
//設(shè)置高質(zhì)量,低速度呈現(xiàn)平滑程度
g.SmoothingMode = SmoothingMode.HighQuality;
//清空畫布并以透明背景色填充
g.Clear(Color.Transparent);
//在指定位置并且按指定大小繪制原圖片的指定部分
g.DrawImage(sourceImage, new Rectangle(0, 0, toWidth, toHeight),
new Rectangle(x, y, ow, oh),
GraphicsUnit.Pixel);
//以下代碼為保存圖片時,設(shè)置壓縮質(zhì)量
ep = new EncoderParameters();
long[] qy = new long[1];
qy[0] = flag;//設(shè)置壓縮的比例1-100
eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
ep.Param[0] = eParam;
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();//獲取圖像編碼器的信息
ImageCodecInfo jpegICIinfo = null;
for (int i = 0; i < arrayICI.Length; i++)
{
if (arrayICI[i].FormatDescription.Equals("JPEG"))
{
jpegICIinfo = arrayICI[i];
break;
}
}
if (jpegICIinfo != null)
{
bitmap.Save(targetPath, jpegICIinfo, ep);
FileInfo fiTarget = new FileInfo(targetPath);
if (size > 0 && fiTarget.Length > 1024 * size)
{
flag = flag - 10;
MakeThumbnail(sourcePath, targetPath, width, height, mode, flag, size);
}
}
else
{
//以jpg格式保存縮略圖
bitmap.Save(targetPath, ImageFormat.Jpeg);
}
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
if (sourceImage != null)
{
sourceImage.Dispose();
}
if (bitmap != null)
{
bitmap.Dispose();
}
if (g != null)
{
g.Dispose();
}
if (ep != null)
{
ep.Dispose();
}
if (eParam != null)
{
eParam.Dispose();
}
}
}
總結(jié)
以上所述是小編給大家介紹的.Net 實現(xiàn)圖片縮略圖上傳通用方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
靈活掌握asp.net中g(shù)ridview控件的多種使用方法(下)
這篇文章繼續(xù)向大家推薦如何靈活掌握asp.net中g(shù)ridview控件的多種使用方法,感興趣的小伙伴們可以參考一下2015-11-11
ASP.NET MVC使用RazorEngine解析模板生成靜態(tài)頁
這篇文章主要介紹了ASP.NET MVC使用RazorEngine解析模板生成靜態(tài)頁的相關(guān)資料,需要的朋友可以參考下2016-05-05
防止在服務(wù)器處理完成之前用戶多次點擊提交按鈕處理代碼
在提交表單時,如果網(wǎng)頁速度過慢或者其他原因,用戶多次提交能導致數(shù)據(jù)的修改,怎么解決這個問題呢,接下來將為您解決這個問題,需要的朋友可以了解下2012-12-12
this connector is disabled錯誤的解決方法
打開editor/filemanager/connectors/aspx/config.ascx修改CheckAuthentication()方法,返回true2008-11-11

