欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-28 12:50:22
复制代码 代码如下:/// /// 获得缩微图/// ///

复制代码 代码如下:

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