简体   繁体   English

从文件夹读取图像时出现内存不足异常

[英]out of memory exception when reading image from folder

i need to read all image files from folder ans save it to another folder with compressed size.However my code compresses this images very well but it give error after 695 image files "out of memory exception". 我需要从文件夹ans中读取所有图像文件,然后将其保存到另一个具有压缩大小的文件夹中。但是我的代码很好地压缩了这些图像,但是在695个图像文件“内存不足异常”之后,它给出了错误。 This my code.there are around 2000 images 这是我的代码。大约有2000张图片

List<string> files = new List<string>();
files = Directory.GetFiles(Server.MapPath("../imgres") + "\\products\\", "*.jpg").ToList();
for (int k = 0; k < files.Count; k++)
{
  if (File.Exists(files[k].ToString()))
  {
    string SaveLocation1 = "";
    System.Drawing.Image thumbnail;
    System.Drawing.Image smallsize;
    System.Drawing.Image originalimg;

    originalimg = System.Drawing.Image.FromFile(files[k].ToString());
    thumbnail = originalimg.GetThumbnailImage(110, 110, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
    smallsize = originalimg.GetThumbnailImage(47, 47, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);

    SaveLocation1 = Server.MapPath("../imgres/products") + "\\Thumbnail\\" + Path.GetFileName(files[k].ToString());
    thumbnail.Save(SaveLocation1);
    thumbnail.Dispose();

    SaveLocation1 = Server.MapPath("../imgres/products") + "\\smallsize\\" + Path.GetFileName(files[k].ToString());
    smallsize.Save(SaveLocation1);
    smallsize.Dispose();

  }
}

The problem is that you are not disposing originalimg like you are with the other image references. 问题在于您不会像处理其他图像引用那样处理originalimg You should add the following at the end of your if statement: 您应该在if语句的末尾添加以下内容:

originalimg.Dispose();

I would however recommend you use using blocks, these will help you better manage the disposing of such resources as you won't have to implicitly call the Dispose method, and it will also handle the dispose even if an exception occurring before your call to Dispose , so you can be sure it will be cleaned up correctly. 但是,我建议您使用using块,因为这些块将帮助您更好地管理此类资源的处理,因为您不必隐式调用Dispose方法,即使在调用Dispose之前发生异常,它也将处理该dispose。 ,因此可以确保将其正确清理。

Something like this: 像这样:

if (File.Exists(files[k].ToString()))
{
    using(System.Drawing.Image originalimg = System.Drawing.Image.FromFile(files[k].ToString()))
    {
        using(System.Drawing.Image thumbnail = originalimg.GetThumbnailImage(110, 110, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero))
        {
            using(System.Drawing.Image smallsize = originalimg.GetThumbnailImage(47, 47, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero))
            {
                string SaveLocation1 = Server.MapPath("../imgres/products") + "\\Thumbnail\\" + Path.GetFileName(files[k].ToString());
                thumbnail.Save(SaveLocation1);

                SaveLocation1 = Server.MapPath("../imgres/products") + "\\smallsize\\" + Path.GetFileName(files[k].ToString());
                smallsize.Save(SaveLocation1);
            }
        }
    }
}

I would recommend to modify this with a using block, to avoid any need to call (or forget to call) the Dispose method: 我建议使用using块对此进行修改,以避免需要调用(或忘记调用) Dispose方法:

using (Image originalimg = Image.FromFile(files[k].ToString()))
{
   using (Image thumbnail = originalimg.GetThumbnailImage(110, 110, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero))
   {
      thumbnail.Save(...);
   }

   using (Image smallsize = originalimg.GetThumbnailImage(47, 47, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero))
   {
      smallsize.Save(...);
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM