c#生成缩略图不失真的方法实例分享

发布时间:2022-04-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了c#生成缩略图不失真的方法实例分享脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

复制代码 代码如下:

/// <summary>
/// 获得缩微图
/// </summary>
/// <returns></returns>
&nbsp; 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;
}
}
@H_360_63@

脚本宝典总结

以上是脚本宝典为你收集整理的c#生成缩略图不失真的方法实例分享全部内容,希望文章能够帮你解决c#生成缩略图不失真的方法实例分享所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: