.Net Image调整内存泄漏

时间:2013-07-23 09:53:45

标签: c#

我正在尝试在批处理作业中调整图像大小。当我使用.Net提供的类时,内存未正确释放,因此抛出OutOfMemoryException。我想我正确地使用了陈述。代码如下:

    private static byte[] Resize(byte[] imageBytes, int width, int height)
    {
            using (var img = Image.FromStream(new MemoryStream(imageBytes)))
            {
                using (var outStream = new MemoryStream())
                {
                    double y = img.Height;
                    double x = img.Width;

                    double factor = 1;
                    if (width > 0)
                        factor = width / x;
                    else if (height > 0)
                        factor = height / y;

                    var imgOut = new Bitmap((int)(x * factor), (int)(y * factor));
                    var g = Graphics.FromImage(imgOut);
                    g.Clear(Color.White);
                    g.DrawImage(img, new Rectangle(0, 0, (int)(factor * x),
                                                   (int)(factor * y)),
                                new Rectangle(0, 0, (int)x, (int)y), GraphicsUnit.Pixel);

                    imgOut.Save(outStream, ImageFormat.Jpeg);

                    return outStream.ToArray();
                }
            }
      }

此代码的替代方法是使用FreeImage库。当我使用FreeImage时,没有内存问题。使用FreeImage的代码:

   private static byte[] Resize(byte[] imageBytes, int width, int height)
   {
        var img = new FIBITMAP();
        var rescaled = new FIBITMAP();
        try
        {
            using (var inStream = new MemoryStream(imageBytes))
            {
                img = FreeImage.LoadFromStream(inStream);
                rescaled = FreeImage.Rescale(img, width, height, FREE_IMAGE_FILTER.FILTER_BICUBIC);

                using (var outStream = new MemoryStream())
                {
                    FreeImage.SaveToStream(rescaled, outStream, FREE_IMAGE_FORMAT.FIF_JPEG);
                    return outStream.ToArray();
                }
            }
        }
        finally
        {
            if (!img.IsNull)
                FreeImage.Unload(img);

            img.SetNull();

            if (!rescaled.IsNull)
                FreeImage.Unload(rescaled);

            rescaled.SetNull();
        }
   }

我的第一段代码中缺少什么?

2 个答案:

答案 0 :(得分:5)

我相信你的泄密有以下两行:

var imgOut = new Bitmap((int)(x * factor), (int)(y * factor));
var g = Graphics.FromImage(imgOut);

BitmapGraphics都会实现IDisposable,因此应在您使用完毕后进行处理。

我建议将它们包装在using块中:

using(imgOut = new Bitmap((int)(x * factor), (int)(y * factor)))
{
    using(var g = Graphics.FromImage(imgOut))
    {
        //rest of code...
    }
}

Here is a list of GDI objects要密切关注,请确保在使用它们时正确清理它们。

答案 1 :(得分:0)

更正确的方法:

private static byte[] Resize(byte[] imageBytes, int width, int height)
    {
        using (var imagestream = new MemoryStream(imageBytes))
        {
            using (var img = Image.FromStream(imagestream))
            {
                using (var outStream = new MemoryStream())
                {
                    double y = img.Height;
                    double x = img.Width;

                    double factor = 1;
                    if (width > 0)
                        factor = width / x;
                    else if (height > 0)
                        factor = height / y;

                    using (var imgOut = new Bitmap((int)(x * factor), (int)(y * factor)))
                    {
                        using (var g = Graphics.FromImage(imgOut))
                        {
                            g.Clear(Color.White);
                            g.DrawImage(img, new Rectangle(0, 0, (int)(factor * x),
                                                   (int)(factor * y)),
                                new Rectangle(0, 0, (int)x, (int)y), GraphicsUnit.Pixel);
                        }

                        imgOut.Save(outStream, ImageFormat.Jpeg);
                    }

                    return outStream.ToArray();
                }
            }
        }

 }

在分配和取消分配大型对象(一个> = 85000字节)时,您还需要非常 ...因为它们会继续运行LOH(大对象堆),并且有可能将它分段,然后比你预期的更快地耗尽内存(如果遇到这个问题,有各种技术可以解决这个问题)。