5

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

    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();
        }
   }

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

4

2 回答 2

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...
    }
}

这是一个需要注意的 GDI 对象列表,如果使用它们,请确保正确清理它们。

于 2013-07-23T09:58:41.857 回答
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(大对象堆)上进行,并且有可能对其进行分段然后用完内存比您期望(如果遇到该问题,有多种技术可以解决该问题)。

于 2013-07-23T10:01:01.390 回答