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;
}
}
相關文章
詳解在Azure上部署Asp.NET Core Web App
這篇文章主要介紹了詳解在Azure上部署Asp.NET Core Web App,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12
ASP.NET導出Excel打開時提示:與文件擴展名指定文件不一致解決方法
ASP.NET導出Excel,打開時提示“您嘗試打開文件'XXX.xls'的格式與文件擴展名指定文件不一致” 很是郁悶,于是搜集了一些解決方法,感興趣的朋友可以了解下2013-01-01
asp.net網站底部的版權信息實現(xiàn)代碼且可維護
網站底部的版權信息在特殊情況還是比較重要的所以在實現(xiàn)的時候一定要盡可能的做到可維護性,接下來將介紹一些技巧可達到可維護效果,感興趣的你可不要錯過了哈2013-02-02
發(fā)布asp.net core時如何修改ASPNETCORE_ENVIRONMENT環(huán)境變量
這篇文章主要介紹了發(fā)布asp.net core時如何修改ASPNETCORE_ENVIRONMENT環(huán)境變量,幫助大家更好的理解和學習使用.net技術,感興趣的朋友可以了解下2021-04-04

