asp.net生成高質(zhì)量縮略圖通用函數(shù)(c#代碼),支持多種生成方式
/// 生成縮略圖
/// </summary>
/// <param name="originalImagePath">源圖路徑(物理路徑)</param>
/// <param name="thumbnailPath">縮略圖路徑(物理路徑)</param>
/// <param name="width">縮略圖寬度</param>
/// <param name="height">縮略圖高度</param>
/// <param name="mode">生成縮略圖的方式</param>
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
Image originalImage = Image.FromFile(originalImagePath);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
switch (mode)
{
case "HW"://指定高寬縮放(可能變形)
break;
case "W"://指定寬,高按比例
toheight = originalImage.Height * width/originalImage.Width;
break;
case "H"://指定高,寬按比例
towidth = originalImage.Width * height/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*height/towidth;
x = 0;
y = (originalImage.Height - oh)/2;
}
break;
default :
break;
}
//新建一個(gè)bmp圖片
Image bitmap = new System.Drawing.Bitmap(towidth,toheight);
//新建一個(gè)畫(huà)板
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//設(shè)置高質(zhì)量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//設(shè)置高質(zhì)量,低速度呈現(xiàn)平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空畫(huà)布并以透明背景色填充
g.Clear(Color.Transparent);
//在指定位置并且按指定大小繪制原圖片的指定部分
g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
new Rectangle(x, y, ow,oh),
GraphicsUnit.Pixel);
try
{
//以jpg格式保存縮略圖
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch(System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
- ASP.NET創(chuàng)建動(dòng)態(tài)縮略圖的方法
- asp.net生成縮略圖示例方法分享
- asp.net中生成縮略圖并添加版權(quán)實(shí)例代碼
- asp.net生成縮略圖實(shí)現(xiàn)代碼
- asp.net文件上傳功能(單文件,多文件,自定義生成縮略圖,水印)
- asp.net 生成縮略圖代碼
- asp.net 上傳圖片并同時(shí)生成縮略圖的代碼
- asp.net 點(diǎn)縮略圖彈出隨圖片大小自動(dòng)調(diào)整的頁(yè)面
- ASP.Net 上傳圖片并生成高清晰縮略圖
- ASP.NET中高質(zhì)量縮略圖的生成代碼
- asp.net圖片上傳生成縮略圖的注意事項(xiàng)
- ASP.NET實(shí)現(xiàn)根據(jù)URL生成網(wǎng)頁(yè)縮略圖的方法
相關(guān)文章
詳解最好的.NET開(kāi)源免費(fèi)ZIP庫(kù)DotNetZip(.NET組件介紹之三)
本篇文章主要介紹了.NET開(kāi)源免費(fèi)ZIP庫(kù)DotNetZip組件的介紹,可以實(shí)現(xiàn)對(duì)文件的壓縮和解壓,有興趣的朋友可以了解一下。2016-12-12ASP.NET?Core?MVC中Form?Tag?Helpers用法介紹
這篇文章介紹了ASP.NET?Core?MVC中Form?Tag?Helpers的用法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02ASP.NET Core模仿中間件方式實(shí)現(xiàn)列表過(guò)濾功能
這篇文章介紹了ASP.NET Core模仿中間件方式實(shí)現(xiàn)列表過(guò)濾功能的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07Asp.net在線備份、壓縮和修復(fù)Access數(shù)據(jù)庫(kù)示例代碼
這篇文章主要介紹了Asp.net如何在線備份、壓縮和修復(fù)Access數(shù)據(jù)庫(kù),需要的朋友可以參考下2014-03-03Visual Studio Debug實(shí)戰(zhàn)教程之基礎(chǔ)入門(mén)
這篇文章主要給大家介紹了關(guān)于Visual Studio Debug實(shí)戰(zhàn)教程之基礎(chǔ)入門(mén)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09如何傳值在2個(gè)頁(yè)面之間 要求不刷新父頁(yè)面,并且不能用Querystring傳值
通過(guò)Cookie,因?yàn)樗瓤梢栽诜?wù)器端對(duì)其進(jìn)行操作,也可在客戶(hù)端對(duì)其進(jìn)行操作但是缺點(diǎn)是不安全,而且有時(shí)客戶(hù)端會(huì)由于安全問(wèn)題禁用Cookie!2008-12-12asp.net下Oracle,SQL Server,Access萬(wàn)能數(shù)據(jù)庫(kù)通用類(lèi)
Oracle,SQL Server,Access萬(wàn)能數(shù)據(jù)庫(kù)通用類(lèi)!,使用asp.net開(kāi)發(fā)多數(shù)據(jù)庫(kù)系統(tǒng)的朋友可以參考下。2010-10-10