调整大小和裁剪,内存不足

时间:2013-07-05 18:46:00

标签: c# bitmap

我有这个小方法,它会调整图像大小并裁剪它,但它已经突然开始给我OutOfMemory Exception,它已经工作了几个小时。我究竟做错了什么?我认为例外是return bmp.Clone(cropArea, bmp.PixelFormat);

    private static Bitmap Resize(Bitmap image, int width, int height)
    {
        double scaleH = (double)height / image.Height;
        double scaleW = (double)width / image.Width;
        double scale = 1.0;
        if (image.Width * scaleH >= width)
            scale = scaleH;
        else if (image.Height * scaleW >= height)
            scale = scaleW;

        var scaleWidth = (int)(image.Width * scale);
        var scaleHeight = (int)(image.Height * scale);

        using (var bmp = new Bitmap((int)scaleWidth, (int)scaleHeight))
        {
            using (var graph = Graphics.FromImage(bmp))
            {
                graph.DrawImage(image, new Rectangle(0, 0, scaleWidth, scaleHeight));
            }
            int xStart = (bmp.Width - width) / 2;
            int yStart = (bmp.Height - height) / 2;
            Rectangle cropArea = new Rectangle(xStart, yStart, width, height);
            return bmp.Clone(cropArea, bmp.PixelFormat);
        }
    }

解决方案这是一个舍入问题,裁剪矩形比自己的图像大

    var scaleWidth = (int)Math.Ceiling(image.Width * scale);
    var scaleHeight = (int)Math.Ceiling(image.Height * scale);

1 个答案:

答案 0 :(得分:1)

最初我认为这是因为原始问题中存在内存泄漏

Bitmap target = new Bitmap(width, height);
...
target = bmp.Clone(cropArea, bmp.PixelFormat);

您需要将克隆分配给temp var,配置实例目标指向然后返回目标。

如果你确实使用了目标来做你想做的事情

temp = bmp.Clone(cropArea, bmp.PixelFormat);
target.Dipose();
target = temp;

但是你注意到不再是问题(你的更新的例子没有这个问题)我可以告诉你的问题中的代码似乎不是问题,你可能有内存泄漏,MSDN已经关于它的更多信息here。如果您提供使用包装非托管资源的对象的代码,我可能能够确定它是否是内存泄漏。

另一种可能性

位图也可能太大,以至于程序可用的内存不足,如果你能告诉我们关于程序失败的位图的更多信息会更容易判断

相关问题