0

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

  }
}
SWeko
  • 30,434
  • 10
  • 71
  • 106
sp_m
  • 2,647
  • 8
  • 38
  • 62

2 Answers2

4

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);
            }
        }
    }
}
Akrem
  • 5,033
  • 8
  • 37
  • 64
musefan
  • 47,875
  • 21
  • 135
  • 185
4

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(...);
   }
}
SWeko
  • 30,434
  • 10
  • 71
  • 106
  • 1
    Even if you don't forget to call it, an exception might get in the way. You should always be ... using `using` #nopunintended. – ta.speot.is Sep 30 '13 at 10:01
  • Thanks for reply guys,problem was with corrupt image some of images were corrupt. – sp_m Oct 04 '13 at 09:35