C# 無損圖片壓縮—— 基於Framework.40 類庫實現

來源:互聯網
上載者:User

標籤:blog   http   color   width   os   cti   

轉載來自:http://write.blog.csdn.net/postedit/7715729

一、測試代碼

    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, "www.****.com");
        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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.