我有一个位图图像,我正试图克隆如下:
Bitmap bmpCrop = bmp.Clone(new System.Drawing.Rectangle(left, top, right - left + 1, bottom - top), bmp.PixelFormat);
有时这一行会抛出OutOfMemoryException类型的异常,所以在克隆之前,我想确保矩形中指定的坐标不在位图的边界之外,因为据我所知,clone((也可能抛出内存不足异常。
我知道我可以通过以下操作获得图像的边界:
GraphicsUnit units = GraphicsUnit.Point;
RectangleF bmpRectangleF = bmp.GetBounds(ref units);
但我不知道如何与Rectanble边界进行比较。
有什么办法吗?
最后我完成了以下工作(感谢Alex K.的建议(:
RectangleF rectangleF = new System.Drawing.Rectangle(left, top, right - left + 1, bottom - top);
GraphicsUnit units = GraphicsUnit.Pixel;
RectangleF bmpRectangleF = bmp.GetBounds(ref units);
if (bmpRectangleF.Contains(rectangleF))
{
Bitmap bmpCrop = bmp.Clone(rectangleF, bmp.PixelFormat);
return (Bitmap)(bmpCrop);
}