当前位置: 移动技术网 > IT编程>开发语言>c# > C#实现为一张大尺寸图片创建缩略图的方法

C#实现为一张大尺寸图片创建缩略图的方法

2019年07月18日  | 移动技术网IT编程  | 我要评论
本文实例讲述了c#实现为一张大尺寸图片创建缩略图的方法。分享给大家供大家参考。具体实现方法如下: public static bitmap createthumb

本文实例讲述了c#实现为一张大尺寸图片创建缩略图的方法。分享给大家供大家参考。具体实现方法如下:

public static bitmap createthumbnail(string lcfilename, int lnwidth, int lnheight)
{
  system.drawing.bitmap bmpout = null;
  try
  {
    bitmap lobmp = new bitmap(lcfilename);
    imageformat loformat = lobmp.rawformat;
    decimal lnratio;
    int lnnewwidth = 0;
    int lnnewheight = 0;
    //*** if the image is smaller than a thumbnail just return it
    if (lobmp.width < lnwidth && lobmp.height < lnheight)
      return lobmp;
    if (lobmp.width > lobmp.height)
    {
      lnratio = (decimal)lnwidth / lobmp.width;
      lnnewwidth = lnwidth;
      decimal lntemp = lobmp.height * lnratio;
      lnnewheight = (int)lntemp;
    }
    else
    {
      lnratio = (decimal)lnheight / lobmp.height;
      lnnewheight = lnheight;
      decimal lntemp = lobmp.width * lnratio;
      lnnewwidth = (int)lntemp;
    }
    bmpout = new bitmap(lnnewwidth, lnnewheight);
    graphics g = graphics.fromimage(bmpout);
    g.interpolationmode = system.drawing.drawing2d.interpolationmode.highqualitybicubic;
    g.fillrectangle(brushes.white, 0, 0, lnnewwidth, lnnewheight);
    g.drawimage(lobmp, 0, 0, lnnewwidth, lnnewheight);
    lobmp.dispose();
  }
  catch
  {
    return null;
  }
  return bmpout;
}

希望本文所述对大家的c#程序设计有所帮助。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网