【问题标题】:C# System.Drawing.Bitmap throwing Out of Memory Exception when cloningC# System.Drawing.Bitmap 克隆时抛出内存不足异常
【发布时间】:2018-07-19 02:48:35
【问题描述】:

我有一个位图图像,我正在尝试克隆如下:

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 bounds 进行比较。

有什么办法吗?

【问题讨论】:

  • bmpRectangleF.Contains() 可以测试。

标签: c# bitmap system.drawing


【解决方案1】:

最后我完成了以下工作(感谢 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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    相关资源
    最近更新 更多