C#實現(xiàn)生成指定圖片的縮略圖
應用場景
我們假設會有如下場景:
場景1:培訓系統(tǒng)中,在上傳課件培訓視頻素材的功能,我們會上傳課程封面圖片,將來會在課程詳情內(nèi)容中在指定的位置輸出。
場景2:人才網(wǎng)站中,企業(yè)端管理后臺,會上傳企業(yè)的 LOGO 內(nèi)容圖片,用于企業(yè)介紹頁面或崗位招聘詳情頁面等。
場景3:商城系統(tǒng)中,商品發(fā)布后臺,會上傳商品的主圖宣傳圖片及其它關(guān)鍵介紹性圖片,用于商品詳情頁面中進行展示、宣傳。
以上等場景都會使用一個通用的功能,查詢。查詢的一個特征點,是會顯示如上場景中涉及的課程封面圖、企業(yè)LOGO圖和商品主宣傳圖。通常為了提高查詢性能顯示效率,會在查詢列表中顯示原有圖片的縮略圖,因為為了達到顯示效果,詳情信息里的圖片畢竟質(zhì)量比較高、尺寸比較大。
因此,生成縮略圖主要要達到以下目的:
1、縮略圖通過壓縮技術(shù)在盡量保證顯示質(zhì)量的情況下,能夠在 Web 瀏覽器中更加迅速地載入數(shù)據(jù)。
2、較小的數(shù)據(jù)量可以節(jié)省流量成本。
3、制作存儲新的縮略圖(僅用于查詢時顯示)可以更加直觀的吸引用戶,提高系統(tǒng)體驗感。
開發(fā)運行環(huán)境
操作系統(tǒng): Windows Server 2019 DataCenter
.net版本: .netFramework4.0 或以上
開發(fā)工具:VS2019 C#
方法設計
public Byte[] MakeThumbnail 方法(制作縮略圖)調(diào)用參數(shù)見如下表格:
序號 | 參數(shù) | 類型 | 說明 |
---|---|---|---|
1 | originalImagePath | string | 物理路徑圖片文件地址,非唯一選項 |
2 | bvalue | Byte[] | Byte[] 類型數(shù)據(jù),非唯一選項 |
3 | thumbnailPath | string | 非必選項,方法返回壓縮后的 Byte[]數(shù)組數(shù)據(jù),如果同時指定輸出文件路徑 thumbnailPath,則同時生成這個文 |
4 | width=0 | int | 指定輸出縮略圖的寬width,默認為0,表示為原圖的寬 |
5 | height=0 | int | 指定輸出縮略圖的高height,默認為0,表示為原圖的高 |
6 | mode | string | mode為壓縮方法:"HW":指定高寬縮放(可能變形),"W":指定寬,高按比例 ,"H":指定高,寬按比例 , "Cut":指定高寬裁減(不變形),參數(shù)默認="Cut" |
7 | interpolationMode | System.Drawing. Drawing2D. InterpolationMode | 指定在縮放或旋轉(zhuǎn)圖像時使用的算法,默認值=System.Drawing.Drawing2D.InterpolationMode.High |
物理路徑文件 originalImagePath 或 Byte[]型數(shù)據(jù) bvalue,兩者同時傳遞以物理路徑文件優(yōu)先。
實現(xiàn)代碼
方法代碼
//制作縮略圖(壓縮圖),可接收兩種參數(shù),物理路徑文件 originalImagePath 或 Byte[]型數(shù)據(jù) bvalue,兩者同時傳遞以物理路徑文件優(yōu)先。 //方法返回壓縮后的 Byte[]數(shù)組數(shù)據(jù),如果同時指定輸出文件路徑thumbnailPath,則同時生成這個文件。 //指定輸出縮略圖的寬width和高height,如果為0,則默認為原圖的寬或高 //mode為壓縮方法:"HW":指定高寬縮放(可能變形),"W":指定寬,高按比例 ,"H":指定高,寬按比例 , "Cut":指定高寬裁減(不變形) public Byte[] MakeThumbnail(string originalImagePath, Byte[] bvalue, string thumbnailPath, int width=0, int height=0, string mode="Cut", System.Drawing.Drawing2D.InterpolationMode interpolationMode= System.Drawing.Drawing2D.InterpolationMode.High) { System.Drawing.Image originalImage; if (originalImagePath != "") { originalImage = System.Drawing.Image.FromFile(originalImagePath); } else { originalImage = System.Drawing.Image.FromStream(new System.IO.MemoryStream(bvalue)); } int towidth = width; int toheight = height; int x = 0; int y = 0; int ow = originalImage.Width; int oh = originalImage.Height; if (towidth == 0) { towidth = ow; } if (toheight == 0) { toheight = oh; } switch (mode) { case "HW"://指定高寬縮放(可能變形) break; case "W"://指定寬,高按比例 toheight = originalImage.Height * towidth / originalImage.Width; break; case "H"://指定高,寬按比例 towidth = originalImage.Width * toheight / originalImage.Height; break; case "Cut"://指定高寬裁減(不變形) if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) { oh = originalImage.Height; ow = originalImage.Height * towidth / toheight; y = 0; x = (originalImage.Width - ow) / 2; } else { ow = originalImage.Width; oh = originalImage.Width * toheight / towidth; x = 0; y = (originalImage.Height - oh) / 2; } break; default: break; } //新建一個bmp圖片 System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一個畫板 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //設置高質(zhì)量插值法 g.InterpolationMode = interpolationMode ; //設置高質(zhì)量,低速度呈現(xiàn)平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //清空畫布并以透明背景色填充 g.Clear(System.Drawing.Color.Transparent); //在指定位置并且按指定大小繪制原圖片的指定部分 g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel); try { //以jpg格式保存縮略圖 System.IO.MemoryStream mstream = new System.IO.MemoryStream(); System.Drawing.Imaging.ImageFormat format = originalImage.RawFormat; System.Drawing.Imaging.ImageFormat toFormat = System.Drawing.Imaging.ImageFormat.Jpeg; if (format.Equals(System.Drawing.Imaging.ImageFormat.Jpeg)) { toFormat = System.Drawing.Imaging.ImageFormat.Jpeg; } else if (format.Equals(System.Drawing.Imaging.ImageFormat.Png)) { toFormat = System.Drawing.Imaging.ImageFormat.Png; } else if (format.Equals(System.Drawing.Imaging.ImageFormat.Bmp)) { toFormat = System.Drawing.Imaging.ImageFormat.Bmp; } else if (format.Equals(System.Drawing.Imaging.ImageFormat.Gif)) { toFormat = System.Drawing.Imaging.ImageFormat.Gif; } else if (format.Equals(System.Drawing.Imaging.ImageFormat.Icon)) { toFormat = System.Drawing.Imaging.ImageFormat.Icon; } else if (format.Equals(System.Drawing.Imaging.ImageFormat.Emf)) { toFormat = System.Drawing.Imaging.ImageFormat.Emf; } else if (format.Equals(System.Drawing.Imaging.ImageFormat.Exif)) { toFormat = System.Drawing.Imaging.ImageFormat.Exif; } else if (format.Equals(System.Drawing.Imaging.ImageFormat.Tiff)) { toFormat = System.Drawing.Imaging.ImageFormat.Tiff; } else if (format.Equals(System.Drawing.Imaging.ImageFormat.Wmf)) { toFormat = System.Drawing.Imaging.ImageFormat.Wmf; } bitmap.Save(mstream, toFormat); byte[] byData = new Byte[mstream.Length]; mstream.Position = 0; mstream.Read(byData, 0, byData.Length); mstream.Close(); if (thumbnailPath != "") { bitmap.Save(thumbnailPath, toFormat); } return byData; } catch (System.Exception e) { throw e; } finally { originalImage.Dispose(); bitmap.Dispose(); g.Dispose(); } }
調(diào)用示例
本調(diào)用示例實現(xiàn)判斷上傳的圖像大小,如果圖像大于2Mb則自動進行壓縮處理。
string upfilename = Request.PhysicalApplicationPath + "\\upload.jpg"; //上傳的圖片路徑 string mtfilename = Request.PhysicalApplicationPath + "\\mt.jpg"; //縮略圖的圖片路徑 if (System.IO.File.Exists(upfilename)) { FileInfo fileInfo = new FileInfo(upfilename); float _fsize = fileInfo.Length / (1024*1024); if (_fsize >= 2) { MakeThumbnail(upfilename, null, mtfilename); } else { mtfilename = upfilename; } Response.Write("Result Filename is :"+mtfilename); }
小結(jié)
輸出縮略圖可以采取動態(tài)輸出和靜態(tài)存儲方式,動態(tài)輸出耗性能,靜態(tài)存儲耗空間,我們可以以空間換時間來獲取更高的性能。我們需要根據(jù)項目的實際情況來決定采用哪種方式比較平衡。
到此這篇關(guān)于C#實現(xiàn)生成指定圖片的縮略圖的文章就介紹到這了,更多相關(guān)C#生成圖片縮略圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!