C#實(shí)現(xiàn)生成指定圖片的縮略圖
應(yīng)用場(chǎng)景
我們假設(shè)會(huì)有如下場(chǎng)景:
場(chǎng)景1:培訓(xùn)系統(tǒng)中,在上傳課件培訓(xùn)視頻素材的功能,我們會(huì)上傳課程封面圖片,將來(lái)會(huì)在課程詳情內(nèi)容中在指定的位置輸出。
場(chǎng)景2:人才網(wǎng)站中,企業(yè)端管理后臺(tái),會(huì)上傳企業(yè)的 LOGO 內(nèi)容圖片,用于企業(yè)介紹頁(yè)面或崗位招聘詳情頁(yè)面等。
場(chǎng)景3:商城系統(tǒng)中,商品發(fā)布后臺(tái),會(huì)上傳商品的主圖宣傳圖片及其它關(guān)鍵介紹性圖片,用于商品詳情頁(yè)面中進(jìn)行展示、宣傳。
以上等場(chǎng)景都會(huì)使用一個(gè)通用的功能,查詢(xún)。查詢(xún)的一個(gè)特征點(diǎn),是會(huì)顯示如上場(chǎng)景中涉及的課程封面圖、企業(yè)LOGO圖和商品主宣傳圖。通常為了提高查詢(xún)性能顯示效率,會(huì)在查詢(xún)列表中顯示原有圖片的縮略圖,因?yàn)闉榱诉_(dá)到顯示效果,詳情信息里的圖片畢竟質(zhì)量比較高、尺寸比較大。
因此,生成縮略圖主要要達(dá)到以下目的:
1、縮略圖通過(guò)壓縮技術(shù)在盡量保證顯示質(zhì)量的情況下,能夠在 Web 瀏覽器中更加迅速地載入數(shù)據(jù)。
2、較小的數(shù)據(jù)量可以節(jié)省流量成本。
3、制作存儲(chǔ)新的縮略圖(僅用于查詢(xún)時(shí)顯示)可以更加直觀的吸引用戶(hù),提高系統(tǒng)體驗(yàn)感。
開(kāi)發(fā)運(yùn)行環(huán)境
操作系統(tǒng): Windows Server 2019 DataCenter
.net版本: .netFramework4.0 或以上
開(kāi)發(fā)工具:VS2019 C#
方法設(shè)計(jì)
public Byte[] MakeThumbnail 方法(制作縮略圖)調(diào)用參數(shù)見(jiàn)如下表格:
| 序號(hào) | 參數(shù) | 類(lèi)型 | 說(shuō)明 |
|---|---|---|---|
| 1 | originalImagePath | string | 物理路徑圖片文件地址,非唯一選項(xiàng) |
| 2 | bvalue | Byte[] | Byte[] 類(lèi)型數(shù)據(jù),非唯一選項(xiàng) |
| 3 | thumbnailPath | string | 非必選項(xiàng),方法返回壓縮后的 Byte[]數(shù)組數(shù)據(jù),如果同時(shí)指定輸出文件路徑 thumbnailPath,則同時(shí)生成這個(gè)文 |
| 4 | width=0 | int | 指定輸出縮略圖的寬width,默認(rèn)為0,表示為原圖的寬 |
| 5 | height=0 | int | 指定輸出縮略圖的高h(yuǎn)eight,默認(rèn)為0,表示為原圖的高 |
| 6 | mode | string | mode為壓縮方法:"HW":指定高寬縮放(可能變形),"W":指定寬,高按比例 ,"H":指定高,寬按比例 , "Cut":指定高寬裁減(不變形),參數(shù)默認(rèn)="Cut" |
| 7 | interpolationMode | System.Drawing. Drawing2D. InterpolationMode | 指定在縮放或旋轉(zhuǎn)圖像時(shí)使用的算法,默認(rèn)值=System.Drawing.Drawing2D.InterpolationMode.High |
物理路徑文件 originalImagePath 或 Byte[]型數(shù)據(jù) bvalue,兩者同時(shí)傳遞以物理路徑文件優(yōu)先。
實(shí)現(xiàn)代碼
方法代碼
//制作縮略圖(壓縮圖),可接收兩種參數(shù),物理路徑文件 originalImagePath 或 Byte[]型數(shù)據(jù) bvalue,兩者同時(shí)傳遞以物理路徑文件優(yōu)先。
//方法返回壓縮后的 Byte[]數(shù)組數(shù)據(jù),如果同時(shí)指定輸出文件路徑thumbnailPath,則同時(shí)生成這個(gè)文件。
//指定輸出縮略圖的寬width和高h(yuǎn)eight,如果為0,則默認(rèn)為原圖的寬或高
//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;
}
//新建一個(gè)bmp圖片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一個(gè)畫(huà)板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//設(shè)置高質(zhì)量插值法
g.InterpolationMode = interpolationMode ;
//設(shè)置高質(zhì)量,低速度呈現(xiàn)平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
//清空畫(huà)布并以透明背景色填充
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)用示例實(shí)現(xiàn)判斷上傳的圖像大小,如果圖像大于2Mb則自動(dòng)進(jìn)行壓縮處理。
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é)
輸出縮略圖可以采取動(dòng)態(tài)輸出和靜態(tài)存儲(chǔ)方式,動(dòng)態(tài)輸出耗性能,靜態(tài)存儲(chǔ)耗空間,我們可以以空間換時(shí)間來(lái)獲取更高的性能。我們需要根據(jù)項(xiàng)目的實(shí)際情況來(lái)決定采用哪種方式比較平衡。
到此這篇關(guān)于C#實(shí)現(xiàn)生成指定圖片的縮略圖的文章就介紹到這了,更多相關(guān)C#生成圖片縮略圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#中的委托數(shù)據(jù)類(lèi)型簡(jiǎn)介
委托是一個(gè)類(lèi)型安全的對(duì)象,它指向程序中另一個(gè)以后會(huì)被調(diào)用的方法(或多個(gè)方法)。通過(guò)本文給大家介紹C#中的委托數(shù)據(jù)類(lèi)型簡(jiǎn)介,對(duì)c委托類(lèi)型相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-03-03
Visual C#類(lèi)的定義及實(shí)現(xiàn)方法實(shí)例解析
這篇文章主要介紹了Visual C#類(lèi)的定義及實(shí)現(xiàn)方法實(shí)例解析,對(duì)于新手來(lái)說(shuō)有不錯(cuò)的借鑒學(xué)習(xí)價(jià)值,需要的朋友可以參考下2014-07-07
unity實(shí)現(xiàn)車(chē)方向盤(pán)轉(zhuǎn)動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)車(chē)方向盤(pán)轉(zhuǎn)動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
C#實(shí)現(xiàn)讀寫(xiě)分離的五種方法小結(jié)
在C#中實(shí)現(xiàn)分離功能通常指的是將不同的邏輯或責(zé)任分配到不同的類(lèi)或組件中,以增強(qiáng)代碼的可讀性、可維護(hù)性和可擴(kuò)展性,這通常涉及到設(shè)計(jì)模式、依賴(lài)注入和接口的使用,下面是一些在C#中實(shí)現(xiàn)分離功能的基本方法,需要的朋友可以參考下2025-03-03
C#跨PC遠(yuǎn)程調(diào)用程序并顯示UI界面
這篇文章主要為大家介紹了使用C#跨PC遠(yuǎn)程調(diào)用程序并顯示UI界面,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05

