首页 > 代码库 > C#缩放和裁剪图片

C#缩放和裁剪图片

在GDI+中,缩放和剪裁可以看作同一个操作,无非就是原始区域的选择不同罢了。空口无凭,先看具体算法可能更好理解。

C#代码  收藏代码
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Drawing;  
  5. using System.Drawing.Drawing2D;  
  6. using System.Drawing.Imaging;  
  7.   
  8. namespace Project  
  9. {  
  10.     class ImageOperation  
  11.     {  
  12.         /// <summary>  
  13.         ///  Resize图片   
  14.         /// </summary>  
  15.         /// <param name="bmp">原始Bitmap </param>  
  16.         /// <param name="newW">新的宽度</param>  
  17.         /// <param name="newH">新的高度</param>  
  18.         /// <param name="Mode">保留着,暂时未用</param>  
  19.         /// <returns>处理以后的图片</returns>  
  20.   
  21.         public static Bitmap ResizeImage(Bitmap bmp, int newW, int newH, int Mode)  
  22.         {  
  23.             try  
  24.             {  
  25.                 Bitmap b = new Bitmap(newW, newH);  
  26.                 Graphics g = Graphics.FromImage(b);  
  27.                 // 插值算法的质量   
  28.                 g.InterpolationMode = InterpolationMode.HighQualityBicubic;  
  29.                 g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);  
  30.                 g.Dispose();  
  31.                 return b;  
  32.             }  
  33.             catch  
  34.             {  
  35.                 return null;  
  36.             }  
  37.         }  
  38.         /// <summary>  
  39.         /// 剪裁 -- 用GDI+   
  40.         /// </summary>  
  41.         /// <param name="b">原始Bitmap</param>  
  42.         /// <param name="StartX">开始坐标X</param>  
  43.         /// <param name="StartY">开始坐标Y</param>  
  44.         /// <param name="iWidth">宽度</param>  
  45.         /// <param name="iHeight">高度</param>  
  46.         /// <returns>剪裁后的Bitmap</returns>  
  47.         public static Bitmap Cut(Bitmap b, int StartX, int StartY, int iWidth, int iHeight)  
  48.         {  
  49.             if (b == null)  
  50.             {  
  51.                 return null;  
  52.             }  
  53.             int w = b.Width;  
  54.             int h = b.Height;  
  55.             if (StartX >= w || StartY >= h)  
  56.             {  
  57.                 return null;  
  58.             }  
  59.             if (StartX + iWidth > w)  
  60.             {  
  61.                 iWidth = w - StartX;  
  62.             }  
  63.             if (StartY + iHeight > h)  
  64.             {  
  65.                 iHeight = h - StartY;  
  66.             }  
  67.             try  
  68.             {  
  69.                 Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);  
  70.                 Graphics g = Graphics.FromImage(bmpOut);  
  71.                 g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);  
  72.                 g.Dispose();  
  73.                 return bmpOut;  
  74.             }  
  75.             catch  
  76.             {  
  77.                 return null;  
  78.             }  
  79.         }  
  80.     }  
  81. }  

 目标其实都是new Rectangle(0, 0, iWidth, iHeight),缩放算法把整个原始图都往目标区域里塞new Rectangle(0, 0, bmp.Width, bmp.Height),而剪裁只是把原始区域上等宽等高的那个区域new Rectangle(StartX, StartY, iWidth, iHeight)1:1的塞到目标区域里。

<iframe id="google_ads_frame5" vspace="0" height="250" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3447371224873639&output=html&h=250&slotname=8660799060&adk=1970350646&w=300&lmt=1402505322&flash=0&url=http%3A%2F%2Fwww.cnblogs.com%2Fgc2013%2Fp%2F3764791.html&dt=1402505323980&shv=r20140603&cbv=r20140417&saldr=sb&correlator=1402505323455&frm=20&ga_vid=469728998.1401679695&ga_sid=1402494257&ga_hid=1073694382&ga_fc=1&u_tz=480&u_his=420&u_java=1&u_h=768&u_w=1364&u_ah=740&u_aw=1364&u_cd=16&u_nplug=0&u_nmime=0&dff=verdana&dfs=12&adx=0&ady=46306&biw=314&bih=74&eid=33995330%2C317150304&oid=3&rx=0&eae=0&docm=9&vis=0&fu=0&ifi=5&xpc=0NuUlKLqr7&p=http%3A//www.cnblogs.com&dtd=49" frameBorder="0" width="300" allowTransparency="true" name="google_ads_frame5" marginWidth="0" scrolling="no" hspace="0"></iframe><iframe id="google_ads_frame6" vspace="0" height="250" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3447371224873639&output=html&h=250&slotname=8660799060&adk=1970350646&w=300&lmt=1402505322&flash=0&url=http%3A%2F%2Fwww.cnblogs.com%2Fgc2013%2Fp%2F3764791.html&dt=1402505324037&shv=r20140603&cbv=r20140417&saldr=sb&prev_slotnames=8660799060&correlator=1402505323455&frm=20&ga_vid=469728998.1401679695&ga_sid=1402494257&ga_hid=1073694382&ga_fc=1&u_tz=480&u_his=420&u_java=1&u_h=768&u_w=1364&u_ah=740&u_aw=1364&u_cd=16&u_nplug=0&u_nmime=0&dff=verdana&dfs=12&adx=304&ady=46556&biw=314&bih=74&eid=33995330%2C317150304&oid=3&rx=0&eae=0&docm=9&vis=0&fu=0&ifi=6&xpc=moMx45HYms&p=http%3A//www.cnblogs.com&dtd=42" frameBorder="0" width="300" allowTransparency="true" name="google_ads_frame6" marginWidth="0" scrolling="no" hspace="0"></iframe>