日期:2014-05-18 浏览次数:21286 次
/// <summary> /// 剪切 /// </summary> /// <param name="b">原始图</param> /// <param name="startX">原始图上的起始坐标X值</param> /// <param name="startY">原始图上的起始坐标Y值</param> /// <param name="iWidth">需要的宽度</param> /// <param name="iHeight">需要的高度</param> /// <returns>结果图</returns> public Bitmap Cut(Bitmap b, int startX, int startY, int iWidth, int iHeight) { if (b == null) { return null; } int w = b.Width; int h = b.Height; if (startX >= w || startY >= h) { return null; } if (startX + iWidth > w) { iWidth = w - startX; } if (startY + iHeight > h) { iHeight = h - startY; } Graphics g = null; try { Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format32bppArgb); g = Graphics.FromImage(bmpOut); g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(startX, startY, iWidth, iHeight), GraphicsUnit.Pixel); return bmpOut; } catch { return null; } finally { if (g != null) g.Dispose(); } }