c#生成縮略圖不失真的方法實(shí)例分享
/// <summary>
/// 獲得縮微圖
/// </summary>
/// <returns></returns>
public bool GetThumbImg()
{
try
{
string imgpath; //原始路徑
if(imgsourceurl.IndexOf("\",0)<0) //使用的是相對(duì)路徑
{
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)//使用的是相對(duì)路徑
{
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ù),感興趣的朋友可以了解下,希望本文對(duì)你有所幫助2013-01-01Entity Framework Core中執(zhí)行SQL語(yǔ)句和存儲(chǔ)過程的方法介紹
這篇文章介紹了Entity Framework Core中執(zhí)行SQL語(yǔ)句和存儲(chǔ)過程的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02ASP.NET 2.0 中的創(chuàng)建母版頁(yè)
ASP.NET 2.0 中的創(chuàng)建母版頁(yè)...2006-09-09vb 中的MD5加密在asp.net中的實(shí)現(xiàn)
給定標(biāo)識(shí)哈希類型的密碼和字符串,該例程產(chǎn)生一個(gè)適合存儲(chǔ)在配置文件中的哈希密碼,感興趣的朋友可以參考下本文2013-04-04ASP.NET MVC阿里大于短信接口開發(fā)短信群發(fā)能
這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC阿里大于短信接口來(lái)開發(fā)例會(huì)短信群發(fā)能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10.net 讀取項(xiàng)目AssemblyInfo.cs屬性值
.net 讀取項(xiàng)目AssemblyInfo.cs屬性值的實(shí)現(xiàn)代碼。2009-06-06ASP.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)簡(jiǎn)單的熔斷降級(jí)
這篇文章主要給大家介紹了關(guān)于如何利用HttpClientFactory實(shí)現(xiàn)簡(jiǎn)單的熔斷降級(jí)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07Asp.net mvc 數(shù)據(jù)調(diào)用示例代碼
Asp.net mvc 數(shù)據(jù)調(diào)用示例代碼,學(xué)習(xí)asp.net mvc框架的朋友可以參考下。2010-10-10