[C#] C#图片剪裁类 →→→→→进入此内容的聊天室

来自 , 2020-05-18, 写在 C#, 查看 159 次.
URL http://www.code666.cn/view/dc49dfeb
  1. public class ImageCut
  2. {
  3.     /// <summary>
  4.  
  5.     /// 剪裁 -- 用GDI+
  6.     /// </summary>
  7.     /// <param name="b">原始Bitmap</param>
  8.     /// <param name="StartX">开始坐标X</param>
  9.     /// <param name="StartY">开始坐标Y</param>
  10.  
  11.     /// <param name="iWidth">宽度</param>
  12.     /// <param name="iHeight">高度</param>
  13.     /// <returns>剪裁后的Bitmap</returns>
  14.  
  15.     public Bitmap KiCut(Bitmap b)
  16.     {
  17.         if (b == null)
  18.         {
  19.             return null;
  20.         }
  21.  
  22.         int w = b.Width;
  23.         int h = b.Height;
  24.  
  25.         if (X >= w || Y >= h)
  26.         {
  27.             return null;
  28.         }
  29.  
  30.         if (X + Width > w)
  31.         {
  32.             Width = w - X;
  33.         }
  34.  
  35.         if (Y + Height > h)
  36.         {
  37.             Height = h - Y;
  38.         }
  39.  
  40.         try
  41.         {
  42.             Bitmap bmpOut = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
  43.  
  44.             Graphics g = Graphics.FromImage(bmpOut);
  45.             g.DrawImage(b, new Rectangle(0, 0, Width, Height), new Rectangle(X, Y, Width, Height), GraphicsUnit.Pixel);
  46.             g.Dispose();
  47.  
  48.             return bmpOut;
  49.         }
  50.         catch
  51.         {
  52.             return null;
  53.         }
  54.     }
  55.  
  56.     public int X = 0;
  57.     public int Y = 0;
  58.     public int Width = 120;
  59.     public int Height = 120;
  60.     public ImageCut(int x, int y, int width, int heigth)
  61.     {
  62.         X = x;
  63.         Y = y;
  64.         Width = width;
  65.         Height = heigth;
  66.     }
  67. }
  68. //csharp/1120

回复 "C#图片剪裁类"

这儿你可以回复上面这条便签

captcha