public class ImageCut { /// /// 剪裁 -- 用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); g.DrawImage(b, new Rectangle(0, 0, Width, Height), new Rectangle(X, Y, Width, Height), GraphicsUnit.Pixel); g.Dispose();
return bmpOut; } catch { return null; } }
public int X = 0; public int Y = 0; public int Width = 120; public int Height = 120; public ImageCut(int x, int y, int width, int heigth) { X = x; Y = y; Width = width; Height = heigth; } }