失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > C#中不失真生成缩略图

C#中不失真生成缩略图

时间:2020-10-06 00:19:39

相关推荐

C#中不失真生成缩略图

C#中不失真生成缩略图--

使用.net的方法GetThumbnailImage生成的缩略图失真严重,这里推荐一种不失真生成缩略图的方法,供参考。

///<summary>

///获得缩微图

///</summary>

///<returns></returns>

publicboolGetThumbImg()

{

try

{

stringimgpath;//原始路径

if(imgsourceurl.IndexOf("\",0)<0)//使用的是相对路径

{

imgpath=HttpContext.Current.Server.MapPath(imgsourceurl);//转化为物理路径

}

else

{

imgpath=imgsourceurl;

}

System.Drawing.ImagesourceImage=System.Drawing.Image.FromFile(imgpath);

intwidth=sourceImage.Width;

intheight=sourceImage.Height;

if(thumbwidth<=0)

{

thumbwidth=120;

}

if(thumbwidth>=width)

{

returnfalse;

}

else

{

(thumbwidth,thHeight*thumbwidth/thWidth,null,IntPtr.Zero);

ImageimgThumb=newSystem.Drawing.Bitmap(thumbwidth,height*thumbwidth/width);

System.Drawing.Graphicsg=System.Drawing.Graphics.FromImage(imgThumb);

g.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

g.DrawImage(sourceImage,newRectangle(0,0,thumbwidth,height*thumbwidth/width),0,0,width,height,GraphicsUnit.Pixel);

stringthumbpath="";

sourceImage.Dispose();

if(thumburl=="")

{

thumbpath=imgpath;

}

if(thumbpath.IndexOf("\",0)<0)//使用的是相对路径

{

thumbpath=HttpContext.Current.Server.MapPath(thumburl);//转化为物理路径

}

imgThumb.Save(thumbpath,ImageFormat.Jpeg);

imgThumb.Dispose();

returntrue;

}

}

catch

{

throw;

}

}

先转了。精辟的文章

如果觉得《C#中不失真生成缩略图》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。
相关阅读
[C#]生成缩略图

[C#]生成缩略图

2018-12-28

C#生成缩略图

C#生成缩略图

2019-02-05

C#生成缩略图源码

C#生成缩略图源码

2021-09-17

C#  生成缩略图 方法

C# 生成缩略图 方法

2021-01-27