ImageResizer out of memory error in a console application

391 views Asked by At

I wrote some code to resize a large number of images using ImageResizer.

foreach(DataRow item in dst.Tables[0].Rows)
{
     string dir = @"C:\Media\ImageCollection\" + item[image];

     string outputDir = @"D:\ImageExport\" + item[filename].ToString().SubString(0, 5);

     if (!Directory.Exists(outputDir))
          Directory.CreateDirectory(outputDir);

     if (Convert.ToInt32(item["width"]) > Convert.ToInt32(item["height"]))
          ImageResizer.ImageBuilder.Current.Build(dir, outputDir + @"\" + item["image"] + ".jpg", new ImageResizer.ResizeSettings("?maxwidth=500&format=jpg&quality=96"), true);
     else
          ImageResizer.ImageBuilder.Current.Build(dir, outputDir + @"\" + item["image"] + ".jpg", new ImageResizer.ResizeSettings("?maxheight=500&format=jpg&quality=96"), true);
}

The code randomly throw the exception Out of Memory in the System.Drawning.Image on one of the ImageResizer.ImageBuilder call.

I am wondering if I am missing something in the AppConfig file.

<configSections>
     <section name="resizer" type="ImageResizer.ResizerSection,ImageResizer"  requirePermission="false"  />
</configSections>  

<resizer>
     <plugins>
          <remove name="SizeLimiting" />
          <add name="PdfRenderer" downloadNativeDependencies="false" />
     </plugins>
</resizer>

Any idea what is happening here?

1

There are 1 answers

3
Lilith River On BEST ANSWER

According to the code you posted, you are trying to use a directory path (@"C:\Media\ImageCollection") as the input image filename. ImageResizer works on 1 image at a time; you need to provide the input image filename instead.

OutOfMemoryException is a generic error message thrown by the Microsoft API whenever something goes wrong; it doesn't always mean there's no memory available.