传入原图,其实坐标点,切割的图像大小,切割图片
/// <summary> /// 切割图片 /// </summary> /// <param name="bit">原图</param> /// <param name="StartX">起始坐标x</param> /// <param name="StartY">起始坐标y</param> /// <param name="iWidth">切割宽度</param> /// <param name="iHeight">切割高度</param> /// <returns></returns> public static Bitmap CutImage(Bitmap bit, int StartX, int StartY, int iWidth, int iHeight) { if (bit == null) { return null; } int w = bit.Width; int h = bit.Height; if (StartX >= w || StartY >= h) { return null; } if (StartX + iWidth > w) { iWidth = w - StartX; } if (StartY + iHeight > h) { iHeight = h - StartY; } try { //此处输出为24位色,可更改为其他位深 Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb); Graphics g = Graphics.FromImage(bmpOut); g.DrawImage(bit, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel); g.Dispose(); return bmpOut; } catch { return null; } }
评论前必须登录!
注册