views:

69

answers:

2

Hi

I'm trying to load about 600 small images into memory. Size of each image file is less then 2 KB (20 x 30). Thus all images are needs at most 5 MB in memory, even with additional information about size, format etc. But after 400th image Image.FromFile() throws OutOfMemory exception.

So, what's the problem?

+5  A: 

The OutOfMemoryException is a bit of a misnomer when you are dealing with the Image.FromFile method. The FromFile method will throw an OutOfMemoryException in some cases where there is actually not an out of memory situation. Namely

  • File does not have a valid image format
  • GDI+ does not support the pixel format

Documentation: http://msdn.microsoft.com/en-us/library/stf701f5.aspx

I think if you debug into this you'll find it's a very specific file which is causing this problem every time. Try removing that file from your list and see if it fixes your problem.

JaredPar
Thanks a lot!It's my fault. I forgot about thumbs.db file.
Pavel Pyhtin
A: 

It's not clear what you're working with, so I'm going to take a stab here...

Keep in mind that if you're loading compressed images, there is also memory overhead when decompressing these. As you're well aware, a 2KB JPG will be much larger as a BMP. When working with these images and thinking about memory requirements, think of the requirements as if they were BMPs rather than JPGs. Then, instead of having 600x2KB (less than 2MB), you really have 600x100KB (60MB), PLUS some of the other misc overhead, which may be very significant (depending on garbage collection and when you dispose objects, the overhead could be 2-3x's the 60MB).

Jaxidian