.Net 實(shí)現(xiàn)圖片縮略圖上傳通用方法
日常開(kāi)發(fā)中,經(jīng)常碰到圖片上傳的需求,尤其在商城系統(tǒng)開(kāi)發(fā)的時(shí)候,商品列表商品圖片展示如果使用高清原圖,由于高清原圖比較大,加載原圖時(shí)間會(huì)大大增加,直接導(dǎo)致系統(tǒng)性能底下,用戶體驗(yàn)不好,并發(fā)量高的時(shí)候直接就掛掉了,這時(shí)候后臺(tái)上傳圖片的時(shí)候,就必須將原高清圖進(jìn)行壓縮,生成高質(zhì)量縮略圖,然后在商品列表讀取縮略圖可以大大減少加載時(shí)間,起到一個(gè)性能優(yōu)化的作用,當(dāng)然在商品詳情的時(shí)候還是得用高清原圖!
以下代碼,可以在實(shí)際開(kāi)發(fā)中使用將圖片高質(zhì)量壓縮,話不多說(shuō),代碼貼下:
/// <summary> /// 生成縮略圖或質(zhì)量壓縮 /// </summary> /// <param name="sourcePath">源圖路徑(物理路徑)</param> /// <param name="targetPath">縮略圖路徑(物理路徑)</param> /// <param name="width">縮略圖寬度,如果寬度為0則不縮略</param> /// <param name="height">縮略圖高度,如果高度為0則不縮略</param> /// <param name="mode">生成縮略圖的方式,默認(rèn)為空,為空則不縮略高寬[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; } } //新建一個(gè)bmp圖片 bitmap = new Bitmap(toWidth, toHeight); //新建一個(gè)畫(huà)板 g = Graphics.FromImage(bitmap); g.CompositingQuality = CompositingQuality.HighQuality; //設(shè)置高質(zhì)量插值法 g.InterpolationMode = InterpolationMode.HighQualityBicubic; //設(shè)置高質(zhì)量,低速度呈現(xiàn)平滑程度 g.SmoothingMode = SmoothingMode.HighQuality; //清空畫(huà)布并以透明背景色填充 g.Clear(Color.Transparent); //在指定位置并且按指定大小繪制原圖片的指定部分 g.DrawImage(sourceImage, new Rectangle(0, 0, toWidth, toHeight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); //以下代碼為保存圖片時(shí),設(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 實(shí)現(xiàn)圖片縮略圖上傳通用方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
靈活掌握asp.net中g(shù)ridview控件的多種使用方法(下)
這篇文章繼續(xù)向大家推薦如何靈活掌握asp.net中g(shù)ridview控件的多種使用方法,感興趣的小伙伴們可以參考一下2015-11-11.NET Web開(kāi)發(fā)之.NET MVC框架介紹
MVC是一種架構(gòu)設(shè)計(jì)模式,該模式主要應(yīng)用于圖形化用戶界面(GUI)應(yīng)用程序。那么什么是MVC?MVC由三部分組成:Model(模型)、View(視圖)及Controller(控制器)2014-03-03ASP.NET MVC使用RazorEngine解析模板生成靜態(tài)頁(yè)
這篇文章主要介紹了ASP.NET MVC使用RazorEngine解析模板生成靜態(tài)頁(yè)的相關(guān)資料,需要的朋友可以參考下2016-05-05Excel自定義關(guān)閉按鈕實(shí)現(xiàn)代碼
這篇文章主要介紹了Excel自定義關(guān)閉按鈕實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12防止在服務(wù)器處理完成之前用戶多次點(diǎn)擊提交按鈕處理代碼
在提交表單時(shí),如果網(wǎng)頁(yè)速度過(guò)慢或者其他原因,用戶多次提交能導(dǎo)致數(shù)據(jù)的修改,怎么解決這個(gè)問(wèn)題呢,接下來(lái)將為您解決這個(gè)問(wèn)題,需要的朋友可以了解下2012-12-12this connector is disabled錯(cuò)誤的解決方法
打開(kāi)editor/filemanager/connectors/aspx/config.ascx修改CheckAuthentication()方法,返回true2008-11-11