.Net 實(shí)現(xiàn)圖片縮略圖上傳通用方法
日常開發(fā)中,經(jīng)常碰到圖片上傳的需求,尤其在商城系統(tǒng)開發(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í)際開發(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è)畫板
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í),設(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ì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
asp.net MVC 在Controller控制器中實(shí)現(xiàn)驗(yàn)證碼輸出功能
這篇文章主要介紹了asp.net MVC 在Controller控制器中實(shí)現(xiàn)驗(yàn)證碼輸出功能,本文給大家介紹非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
asp.net 讀取文本文件并插入數(shù)據(jù)庫(kù)的實(shí)現(xiàn)代碼
最近我司和招行有合作,招行給財(cái)務(wù)的是一個(gè)txt格式的賬務(wù)文本文件,文本文件包含很多內(nèi)容,對(duì)賬只需要用到其中一部分內(nèi)容。2010-04-04
基于SignalR的消息推送與二維碼掃描登錄實(shí)現(xiàn)代碼
這篇文章主要介紹了基于SignalR的消息推送與二維碼掃描登錄實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-02-02
ASP.NET調(diào)用WebService服務(wù)的方法詳解
這篇文章主要介紹了ASP.NET調(diào)用WebService服務(wù)的方法,較為詳細(xì)的分析了WebService服務(wù)的功能,創(chuàng)建步驟與使用方法,需要的朋友可以參考下2016-05-05
Asp.net實(shí)現(xiàn)MVC處理文件的上傳下載功能實(shí)例教程
這篇文章主要介紹了Asp.net實(shí)現(xiàn)MVC處理文件的上傳下載功能,比較全面而系統(tǒng)的對(duì)Asp.net MVC的文件上傳下載功能進(jìn)行了深入分析,有很好的借鑒價(jià)值,需要的朋友可以參考下2014-08-08
ADO.NET實(shí)現(xiàn)對(duì)SQL Server數(shù)據(jù)庫(kù)的增刪改查示例
本篇文章主要介紹了ADO.NET實(shí)現(xiàn)對(duì)SQL Server數(shù)據(jù)庫(kù)的增刪改查示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-01-01
解析如何利用一個(gè)ASP.NET Core應(yīng)用來(lái)發(fā)布靜態(tài)文件
本文主要通過一些簡(jiǎn)單的實(shí)例來(lái)體驗(yàn)一下如何在一個(gè)ASP.NET Core應(yīng)用中發(fā)布靜態(tài)文件。針對(duì)不同格式的靜態(tài)文件請(qǐng)求的處理,ASP.NET Core為我們提供了三個(gè)中間件,它們將是本系列文章論述的重點(diǎn)。有需要的朋友可以看下2016-12-12

