using System; using System.Collections.Generic; using System.Drawing.Imaging; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ShenTun.ImageCollection.Common { public class ImageCut { #region 剪裁图片方法 /// /// 剪裁 -- 用GDI+ /// /// 原始Bitmap,即需要裁剪的图片 /// 开始坐标X /// 开始坐标Y /// 宽度 /// 高度 /// 剪裁后的Bitmap public Bitmap KiCut(Bitmap b) { if (b == null) { return null; } int w = b.Width; int h = b.Height; if (X >= w || Y >= h) { return null; } if (X + Width > w) { Width = w - X; } if (Y + Height > h) { Height = h - Y; } try { Bitmap bmpOut = new Bitmap(Width, Height, PixelFormat.Format24bppRgb); Graphics g = Graphics.FromImage(bmpOut); // Create rectangle for displaying image. Rectangle destRect = new Rectangle(0, 0, Width, Height); //所画的矩形正确,它指定所绘制图像的位置和大小。 将图像进行缩放以适合该矩形。 // Create rectangle for source image. Rectangle srcRect = new Rectangle(X, Y, Width, Height); //srcRect参数指定要绘制的 image 对象的矩形部分。 将此部分进行缩放以适合 destRect 参数所指定的矩形。 g.DrawImage(b, destRect, srcRect, GraphicsUnit.Pixel); //resultG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, side, side), new System.Drawing.Rectangle(0, 0, initWidth, initHeight), System.Drawing.GraphicsUnit.Pixel); g.Dispose(); return bmpOut; } catch { return null; } } #endregion #region ImageCut1类的构造函数 public int X; public int Y; public int Width; public int Height; /// /// ImageCut1类的构造函数,ImageCut1类用来获取鼠标在pictureBox1控件所画矩形内的图像 /// /// /// /// /// public ImageCut(int x, int y, int width, int heigth) { X = x; Y = y; Width = width; Height = heigth; } #endregion } }