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

所属分类: 网络编程 / ASP.NET 阅读数: 1799
收藏 0 赞 0 分享

生成缩略图是一个十分常用功能,找到了一个方法,重写部分代码,实用又好用,.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();
    }

  }
}

更多精彩内容其他人还在看

开源跨平台运行服务插件TaskCore.MainForm

这篇文章主要为大家详细介绍了开源跨平台运行服务插件TaskCore.MainForm的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

CKEditor自定义按钮插入服务端图片

这篇文章主要为大家详细介绍了CKEditor自定义按钮插入服务端图片的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Asp.net Web Api实现图片点击式图片验证码功能

现在验证码的形式越来越丰富,今天要实现的是在点击图片中的文字来进行校验的验证码。下面通过本文给大家分享Asp.net Web Api实现图片点击式图片验证码功能,需要的的朋友参考下吧
收藏 0 赞 0 分享

WPF实现ScrollViewer滚动到指定控件处

这篇文章主要为大家详细介绍了WPF实现ScrollViewer滚动到指定控件处,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

WPF实现带全选复选框的列表控件

这篇文章主要为大家详细介绍了WPF实现带全选复选框的列表控件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Asp.net MVC 中利用jquery datatables 实现数据分页显示功能

这篇文章主要介绍了Asp.net MVC 中利用jquery datatables 实现数据分页显示功能,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

asp.net 利用NPOI导出Excel通用类的方法

本篇文章主要介绍了asp.net 利用NPOI导出Excel通用类的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

VS2015自带LocalDB数据库用法详解

这篇文章主要为大家详细介绍了VS2015自带LocalDB数据库的用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

SignalR Self Host+MVC等多端消息推送服务(一)

这篇文章主要为大家详细介绍了SignalR Self Host+MVC等多端消息推送服务,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

SignalR Self Host+MVC等多端消息推送服务(二)

这篇文章主要为大家详细介绍了SignalR Self Host+MVC等多端消息推送服务的第二篇,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多