博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 无损图片压缩—— 基于Framework.40 类库实现
阅读量:5132 次
发布时间:2019-06-13

本文共 3716 字,大约阅读时间需要 12 分钟。

分为两部分,一个是操作类,另外一个是测试。直接上代码了

 

一、测试代码

    private void button1_Click(object sender, EventArgs e)

    {
      string newSourcePath = ImgPath;//源图存放目录
      string newNewDir = MakePath;   //新图存放目录

      string sourceFile = Path.Combine(ImgPath, "app1.jpg"); //获取原图路径

      string newFile = string.Empty; //新图路径

      ImgThumbnail iz = new ImgThumbnail();

      Action<ImgThumbnail.ImgThumbnailType> Thumbnail = (type =>
        {
          //生成质量
          for (int i = 100; i >= 10; i -= 10)
          {
            //计算新图路径,测试命名:原图名_new_缩放类型_压缩质量.jpg
            newFile = Path.Combine(newNewDir, string.Format("app1_new_{1}_{0}.jpg", i, type.ToString()));
            //压缩图片
            iz.Thumbnail(sourceFile, newFile, 100, 100, i, type);
          }
        });
     
      Thumbnail(ImgThumbnail.ImgThumbnailType.Cut);//裁剪压缩(裁剪压缩还不好用,待调整!)
      Thumbnail(ImgThumbnail.ImgThumbnailType.H);  //以高度为参照压缩,不变形
      Thumbnail(ImgThumbnail.ImgThumbnailType.W);  //以宽度为参照压缩,不变形
      Thumbnail(ImgThumbnail.ImgThumbnailType.WH); //固定宽高压缩,变形
      MessageBox.Show("完成");
    }

 

 

二、操作类

using System.Drawing;

using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace MagickNet.App

{
  /// <summary>
  /// 图片压缩
  /// </summary>
  public class ImgThumbnail
  {

    #region 枚举
    /// <summary>
    /// 指定缩放类型
    /// </summary>
    public enum ImgThumbnailType
    {
      /// <summary>
      /// 无
      /// </summary>
      Nothing=0,
      /// <summary>
      /// 指定高宽缩放(可能变形)
      /// </summary>
      WH = 1,
      /// <summary>
      /// 指定宽,高按比例
      /// </summary>
      W = 2,
      /// <summary>
      /// 指定高,宽按比例
      /// </summary>
      H = 3,
      /// <summary>
      /// 指定高宽裁减(不变形)
      /// </summary>
      Cut = 4,
      /// <summary>
      /// 按照宽度成比例缩放后,按照指定的高度进行裁剪
      /// </summary>
      W_HCut = 5,
    }
    #endregion   

    #region Thumbnail

   

    /// <summary>

    /// 无损压缩图片
    /// </summary>
    /// <param name="sourceFile">原图片</param>
    /// <param name="stream">压缩后保存到流中</param>
    /// <param name="height">高度</param>
    /// <param name="width"></param>
    /// <param name="quality">压缩质量 1-100</param>
    /// <param name="type">压缩缩放类型</param>
    /// <returns></returns>
    private static Bitmap Thumbnail(string sourceFile, int width, int height, int quality
      , ImgThumbnailType type, out ImageFormat tFormat)
    {
      using (System.Drawing.Image iSource = System.Drawing.Image.FromFile(sourceFile))
      {
        tFormat = iSource.RawFormat;
        //缩放后的宽度和高度
        int toWidth = width;
        int toHeight = height;
        //
        int x = 0;
        int y = 0;
        int oWidth = iSource.Width;
        int oHeight = iSource.Height;

        switch (type)

        {
          case ImgThumbnailType.WH://指定高宽缩放(可能变形)           
            {
              break;
            }
          case ImgThumbnailType.W://指定宽,高按比例     
            {
              toHeight = iSource.Height * width / iSource.Width;
              break;
            }
          case ImgThumbnailType.H://指定高,宽按比例
            {
              toWidth = iSource.Width * height / iSource.Height;
              break;
            }
          case ImgThumbnailType.Cut://指定高宽裁减(不变形)     
            {
              if ((double)iSource.Width / (double)iSource.Height > (double)toWidth / (double)toHeight)
              {
                oHeight = iSource.Height;
                oWidth = iSource.Height * toWidth / toHeight;
                y = 0;
                x = (iSource.Width - oWidth) / 2;
              }
              else
              {
                oWidth = iSource.Width;
                oHeight = iSource.Width * height / toWidth;
                x = 0;
                y = (iSource.Height - oHeight) / 2;
              }
              break;
            }
          case ImgThumbnailType.W_HCut://按照宽度成比例缩放后,按照指定的高度进行裁剪
            {
              toHeight = iSource.Height * width / iSource.Width;
              if (height < toHeight)
              {
                oHeight = oHeight * height / toHeight;
                toHeight = toHeight * height / toHeight;
              }
              break;
            }
          default:
            break;
        }

        Bitmap ob = new Bitmap(toWidth, toHeight);

        //ImgWaterMark iwm = new ImgWaterMark();
        //iwm.AddWaterMark(ob, towidth, toheight, "");
        Graphics g = Graphics.FromImage(ob);
        g.Clear(System.Drawing.Color.WhiteSmoke);
        g.CompositingQuality = CompositingQuality.HighQuality;
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.DrawImage(iSource
          , new Rectangle(x, y, toWidth, toHeight)
          , new Rectangle(0, 0, oWidth, oHeight)
          , GraphicsUnit.Pixel);
        g.Dispose();

        return ob;

       
      }
    }

    #endregion
  }
}

 

转载请保留:http://write.blog.csdn.net/postedit/7715729

转载于:https://www.cnblogs.com/xxj-jing/archive/2012/07/04/2890059.html

你可能感兴趣的文章
『C#基础』C#导出Excel
查看>>
int和Integer的区别
查看>>
jquery 获取子元素的限制jquery
查看>>
ABAP Util代码
查看>>
C#中各种计时器
查看>>
python全栈 操作系统
查看>>
精确光源(Punctual Light Sources)
查看>>
数据迁移的应用场景与解决方案Hamal
查看>>
Docker容器的原理与实践 (下)
查看>>
poj 3750 小孩报数问题
查看>>
代码块。 单例
查看>>
HDU3591找零,背包
查看>>
摆脱jquery,用自己的JS库实现ajax功能
查看>>
win10 redis安装教程
查看>>
springboot整合curator实现分布式锁
查看>>
关于渐变属 gradient:
查看>>
android 入门-android Studio 配置
查看>>
字节流
查看>>
个人冲刺八
查看>>
Spring Security(09)——Filter
查看>>