views:

2303

answers:

3

Hi there,

I have an image uploader and cropper which creates thumbnails and I occasionally get an Out Of Memory exception on the following line:

Dim bm As Bitmap = System.Drawing.Image.FromFile(imageFile)

The occurance of the error is tiny and very rare, but I always like to know what might be causing it. The imageFile variable is just a Server.MapPath to the path of the image.

I was curious if anyone had experience this issue previously and if they had any ideas what might be causing it? Is it the size of the image perhaps?

I can post the code if necessary and any supporting information I have, but would love to hear people's opinions on this one.

+2  A: 

It's worth knowing that OutOfMemoryException doesn't always really mean it's out of memory - particularly not when dealing with files. I believe it can also happen if you run out of handles for some reason.

Are you disposing of all your bitmaps after you're done with them? Does this happen repeatably for a single image?

Jon Skeet
Hi Jon, thanks for the quick reply. Yes, I dispose of both the image and the graphics. Dim bmPhoto As New Bitmap(targetW, targetH, PixelFormat.Format24bppRgb)Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)bmPhoto.Dispose()bmPhoto = NothinggrPhoto.Dispose()grPhoto = NothingAs for the repeatability, no it is exceedingly random - but the images always tend to be larger (though no bigger than 700k~).
Chris Laythorpe
I suspect that it may not be file size, but in-memory image size - a very heavily compressed image with a massive number of pixels might cause you issues...
Jon Skeet
Hi Jon, thanks for the comment. Turns out to be a damaged image all along. *sigh*. An incompetent user. Thanks for your answers though :)
Chris Laythorpe
How did you determine that the image was corrupted? I'm also having these OutOfMemoryException errors when doing Image.FromFile. What's peculiar is that it only throws the exception on one server, and not another. Could I be missing some essential encoder? This is a 4256x2832 24bpp sRGB JPEG downloaded from Getty images.
Mark Richman
+2  A: 

If this wasn't a bad image file but was in fact the normal issue with Image.FromFile wherein it leaves file handles open, then the solution is use Image.FromStream instead.

using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
   using (Image original = Image.FromStream(fs))
   {
      ...

Using an explicit Dispose(), a using() statement or setting the value to null doesn't solve the issue until a garbage collection happens, and forcing a garbage collection to happen is generally a bad idea.

So if you App runs for a time and opens a lot of files consider using Image.FromStream() instead.

Hightechrider
A: 

Also you can open it in read mode, (if you want to use it in two place same time)

 public Image OpenImage(string previewFile)
        {
            FileStream fs = new FileStream(previewFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            return Image.FromStream(fs);
        }
Avram