当前位置: 移动技术网 > IT编程>开发语言>.net > c#生成缩略图不失真的方法实例分享

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

陈以庭,麦酷网,重生之喜乐大清

复制代码 代码如下:

/// <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;
}
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网