System.OutOfMemoryException发生在具有大量图片的System.Drawing.dll中

时间:2017-06-24 13:59:12

标签: c# .net

我有一个高度为geolocated文件的文件夹,我想用地标制作一个KML。 没有问题的XML(KML)文档,但图像很大,我想在分隔文件夹中创建一个拇指(1/10)和小图像(1/4)。
图像是4000x3000全面5mb,少量没有问题,但当文件夹包含更多的35图像(在调试中)我有问题。 我尝试使用With使用的一次性对象,GC.collect仅用于图像细化,但没有。

我有1400个图像或更多的文件夹......可以用这个应用程序吗?

public static class MyClass_img
{
    public static void ResizeImage(Image image, int width, int height, string pathImage)
    {
        using (var destImage = new Bitmap(width, height))
        {
            destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

            using (var graphics = Graphics.FromImage(destImage))
            {
                graphics.CompositingMode = CompositingMode.SourceCopy;
                graphics.CompositingQuality = CompositingQuality.HighQuality;
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.SmoothingMode = SmoothingMode.HighQuality;
                graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

                using (var wrapMode = new ImageAttributes())
                {
                    wrapMode.SetWrapMode(WrapMode.TileFlipXY);

                    var destRect = new Rectangle(0, 0, width, height);
                    graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
                }
            }

            destImage.Save(pathImage);
        }
        GC.Collect();
    }

    public static string imageReduce(Image img, string path, int percentValue, string extension, string folder)
    {
        var dirName = new DirectoryInfo(path).Parent.Name;
        string pathImg = folder + dirName + "_" + Path.GetFileNameWithoutExtension(path) + extension;
        AprItalia_img.ResizeImage(img, img.Width / percentValue, img.Height / percentValue, pathImg);
        return pathImg;
    }

}

enter image description here

1 个答案:

答案 0 :(得分:0)

  

您不必调用GC.Collect()。

根据MSDN

  

在将最后一个参考>发布到图像之前,请务必调用Dispose。否则,在垃圾收集器调用Image>对象的Finalize方法

之前,将不会释放它正在使用的资源。

所以在你的情况下,它似乎是为了finalize。你也是通过调用它的dispose释放Image对象吗?