c#生成縮略圖不失真的方法實(shí)例分享
更新時間:2013年12月26日 16:41:27 作者:
使用.net的方法GetThumbnailImage生成的縮略圖失真嚴(yán)重,這里推薦一種不失真生成縮略圖的方法
復(fù)制代碼 代碼如下:
/// <summary>
/// 獲得縮微圖
/// </summary>
/// <returns></returns>
public bool GetThumbImg()
{
try
{
string imgpath; //原始路徑
if(imgsourceurl.IndexOf("\",0)<0) //使用的是相對路徑
{
imgpath = HttpContext.Current.Server.MapPath(imgsourceurl); //轉(zhuǎn)化為物理路徑
}
else
{
imgpath=imgsourceurl;
}
System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(imgpath);
int width = sourceImage.Width;
int height = sourceImage.Height;
if(thumbwidth <= 0)
{
thumbwidth = 120;
}
if(thumbwidth >= width)
{
return false;
}
else
{
(thumbwidth,thHeight*thumbwidth/thWidth,null,IntPtr.Zero);
Image imgThumb=new System.Drawing.Bitmap(thumbwidth,height*thumbwidth/width);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(imgThumb);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(sourceImage, new Rectangle(0, 0, thumbwidth,height*thumbwidth/width), 0, 0, width, height, GraphicsUnit.Pixel);
string thumbpath="";
sourceImage.Dispose();
if(thumburl=="")
{
thumbpath=imgpath;
}
if(thumbpath.IndexOf("\",0)<0)//使用的是相對路徑
{
thumbpath=HttpContext.Current.Server.MapPath(thumburl);//轉(zhuǎn)化為物理路徑
}
imgThumb.Save(thumbpath,ImageFormat.Jpeg);
imgThumb.Dispose();
return true;
}
}
catch
{
throw;
}
}
相關(guān)文章
jQuery調(diào)用WebService返回JSON數(shù)據(jù)及參數(shù)設(shè)置注意問題
.NET Framework 3.5的發(fā)布解決了WebService調(diào)用中json問題,本文將介紹jQuery調(diào)用基于.NET Framework 3.5的WebService返回JSON數(shù)據(jù),感興趣的朋友可以了解下,希望本文對你有所幫助2013-01-01
Entity Framework Core中執(zhí)行SQL語句和存儲過程的方法介紹
這篇文章介紹了Entity Framework Core中執(zhí)行SQL語句和存儲過程的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02
vb 中的MD5加密在asp.net中的實(shí)現(xiàn)
給定標(biāo)識哈希類型的密碼和字符串,該例程產(chǎn)生一個適合存儲在配置文件中的哈希密碼,感興趣的朋友可以參考下本文2013-04-04
ASP.NET MVC阿里大于短信接口開發(fā)短信群發(fā)能
這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC阿里大于短信接口來開發(fā)例會短信群發(fā)能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10
ASP.net處理XML數(shù)據(jù)實(shí)例淺析
這篇文章主要介紹了ASP.net處理XML數(shù)據(jù)實(shí)例淺析,分析了XML的原理與用法,并以實(shí)例形式講述了asp.net處理XML數(shù)據(jù)的方法,需要的朋友可以參考下2014-10-10
如何利用HttpClientFactory實(shí)現(xiàn)簡單的熔斷降級
這篇文章主要給大家介紹了關(guān)于如何利用HttpClientFactory實(shí)現(xiàn)簡單的熔斷降級的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
Asp.net mvc 數(shù)據(jù)調(diào)用示例代碼
Asp.net mvc 數(shù)據(jù)調(diào)用示例代碼,學(xué)習(xí)asp.net mvc框架的朋友可以參考下。2010-10-10

