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

.net等比缩放生成缩略图的方法

程序员文章站 2023-12-18 16:11:52
生成缩略图是一个十分常用功能,找到了一个方法,重写部分代码,实用又好用,.net又一个生成缩略图的方法,不变形 /// ...

生成缩略图是一个十分常用功能,找到了一个方法,重写部分代码,实用又好用,.net又一个生成缩略图的方法,不变形

/// <summary> 
   /// 为图片生成缩略图
   /// </summary> 
   /// <param name="phypath">原图片的路径</param> 
   /// <param name="width">缩略图宽</param> 
   /// <param name="height">缩略图高</param> 
   /// <returns></returns> 
   public system.drawing.image gethvtthumbnail(system.drawing.image image, int width, int height)
   {
     bitmap m_hovertreebmp = new bitmap(width, height);
     //从bitmap创建一个system.drawing.graphics 
     graphics m_hvtgr = graphics.fromimage(m_hovertreebmp);
     //设置 
     m_hvtgr.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
     //下面这个也设成高质量 
     m_hvtgr.compositingquality = system.drawing.drawing2d.compositingquality.highquality;
     //下面这个设成high 
     m_hvtgr.interpolationmode = system.drawing.drawing2d.interpolationmode.highqualitybicubic;
     //把原始图像绘制成上面所设置宽高的缩小图 
     rectangle rectdestination = new rectangle(0, 0, width, height);

     int m_width, m_height;
     if (image.width * height > image.height * width)
     {
       m_height = image.height;
       m_width = (image.height * width) / height;
     }
     else
     {
       m_width = image.width;
       m_height = (image.width * height) / width;
     }

     m_hvtgr.drawimage(image, rectdestination, 0, 0, m_width, m_height, graphicsunit.pixel);

     return m_hovertreebmp;
   }

c#缩略图生成类,采用高质量插值法实现缩略图生成,高质量,低速度呈现平滑程度,可以保持缩略图纵横比,在获取缩略图的时候一开始就根据百分比获取图片的尺寸、根据设定的大小返回图片的大小,并高质量保存缩略图图片,为原图片设置encoderparameters 对象。

以下为类文件,建议保存文件名为:imagehelper.cs

using system;
using system.collections.generic;
using system.drawing;
using system.drawing.imaging;
namespace htmlsnap
{
  public static class imagehelper
  {
    /// 获取缩略图
    public static image getthumbnailimage(image image, int width, int height)
    {
      if (image == null || width < 1 || height < 1)
        return null;
      // 新建一个bmp图片
      image bitmap = new system.drawing.bitmap(width, height);

      // 新建一个画板
      using (graphics g = system.drawing.graphics.fromimage(bitmap))
      {

        // 设置高质量插值法
        g.interpolationmode = system.drawing.drawing2d.interpolationmode.highqualitybicubic;

        // 设置高质量,低速度呈现平滑程度
        g.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
        
        // 高质量、低速度复合
        g.compositingquality = system.drawing.drawing2d.compositingquality.highquality;
        
 // 清空画布并以透明背景色填充
        g.clear(color.transparent);
        
 // 在指定位置并且按指定大小绘制原图片的指定部分
        g.drawimage(image, new rectangle(0, 0, width, height),
          new rectangle(0, 0, image.width, image.height),
          graphicsunit.pixel);
        return bitmap;
      }
    }
    /// <summary>
    /// 生成缩略图,并保持纵横比
    /// </summary>
    /// <returns>生成缩略图后对象</returns>
    public static image getthumbnailimagekeepratio(image image, int width, int height)
    {
      size imagesize = getimagesize(image, width, height);
      return getthumbnailimage(image, imagesize.width, imagesize.height);
    }

    /// <summary>
    /// 根据百分比获取图片的尺寸
    /// </summary>
    public static size getimagesize(image picture, int percent)
    {
      if (picture == null || percent < 1)
        return size.empty;

      int width = picture.width * percent / 100;
      int height = picture.height * percent / 100;

      return getimagesize(picture, width, height);
    }
    /// <summary>
    /// 根据设定的大小返回图片的大小,考虑图片长宽的比例问题
    /// </summary>
    public static size getimagesize(image picture, int width, int height)
    {
      if (picture == null || width < 1 || height < 1)
        return size.empty;
      size imagesize;
      imagesize = new size(width, height);
      double heightratio = (double)picture.height / picture.width;
      double widthratio = (double)picture.width / picture.height;
      int desiredheight = imagesize.height;
      int desiredwidth = imagesize.width;
      imagesize.height = desiredheight;
      if (widthratio > 0)
        imagesize.width = convert.toint32(imagesize.height * widthratio);
      if (imagesize.width > desiredwidth)
      {
        imagesize.width = desiredwidth;
        imagesize.height = convert.toint32(imagesize.width * heightratio);
      }
      return imagesize;
    }
    /// <summary>
    /// 获取图像编码解码器的所有相关信息
    /// </summary>
    /// <param name="mimetype">包含编码解码器的多用途网际邮件扩充协议 (mime) 类型的字符串</param>
    /// <returns>返回图像编码解码器的所有相关信息</returns>
    public static imagecodecinfo getcodecinfo(string mimetype)
    {
      imagecodecinfo[] codecinfo = imagecodecinfo.getimageencoders();
      foreach (imagecodecinfo ici in codecinfo)
      {
        if (ici.mimetype == mimetype) return ici;
      }
      return null;
    }
    public static imagecodecinfo getimagecodecinfo(imageformat format)
    {
      imagecodecinfo[] encoders = imagecodecinfo.getimageencoders();
      foreach (imagecodecinfo icf in encoders)
      {
        if (icf.formatid == format.guid)
        {
          return icf;
        }
      }
      return null;
    }
    public static void saveimage(image image, string savepath, imageformat format)
    {
      saveimage(image, savepath, getimagecodecinfo(format));
    }
    /// <summary>
    /// 高质量保存图片
    /// </summary>
    private static void saveimage(image image, string savepath, imagecodecinfo ici)
    {
      // 设置 原图片 对象的 encoderparameters 对象
      encoderparameters parms = new encoderparameters(1);
      encoderparameter parm = new encoderparameter(encoder.quality, ((long)95));
      parms.param[0] = parm;
      image.save(savepath, ici, parms);
      parms.dispose();
    }

  }
}

上一篇:

下一篇: