繁体   English   中英

克隆时C#System.Drawing.Bitmap抛出内存不足异常

[英]C# System.Drawing.Bitmap throwing Out of Memory Exception when cloning

我有一个要克隆的位图图像,如下所示:

Bitmap bmpCrop = bmp.Clone(new System.Drawing.Rectangle(left, top, right - left + 1, bottom - top), bmp.PixelFormat);

有时此行会引发OutOfMemoryException类型的异常,因此在克隆之前,我想确保Rectangle中指定的坐标不在位图的范围之内,因为据我所知,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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM