c#生成縮略圖不失真的方法實例分享
更新時間:2013年12月26日 16:41:27 作者:
使用.net的方法GetThumbnailImage生成的縮略圖失真嚴重,這里推薦一種不失真生成縮略圖的方法
復制代碼 代碼如下:
/// <summary>
/// 獲得縮微圖
/// </summary>
/// <returns></returns>
public bool GetThumbImg()
{
try
{
string imgpath; //原始路徑
if(imgsourceurl.IndexOf("\",0)<0) //使用的是相對路徑
{
imgpath = HttpContext.Current.Server.MapPath(imgsourceurl); //轉化為物理路徑
}
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);//轉化為物理路徑
}
imgThumb.Save(thumbpath,ImageFormat.Jpeg);
imgThumb.Dispose();
return true;
}
}
catch
{
throw;
}
}
相關文章
jQuery調用WebService返回JSON數(shù)據(jù)及參數(shù)設置注意問題
.NET Framework 3.5的發(fā)布解決了WebService調用中json問題,本文將介紹jQuery調用基于.NET Framework 3.5的WebService返回JSON數(shù)據(jù),感興趣的朋友可以了解下,希望本文對你有所幫助2013-01-01Entity Framework Core中執(zhí)行SQL語句和存儲過程的方法介紹
這篇文章介紹了Entity Framework Core中執(zhí)行SQL語句和存儲過程的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02ASP.NET MVC阿里大于短信接口開發(fā)短信群發(fā)能
這篇文章主要為大家詳細介紹了ASP.NET MVC阿里大于短信接口來開發(fā)例會短信群發(fā)能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10如何利用HttpClientFactory實現(xiàn)簡單的熔斷降級
這篇文章主要給大家介紹了關于如何利用HttpClientFactory實現(xiàn)簡單的熔斷降級的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-07-07