简体   繁体   中英

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". This my code.there are around 2000 images

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. You should add the following at the end of your if statement:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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