bitmap memory limit?

T

Tony Hsieh

Hi,

When I try to load anything over 134 bitmaps of size 240x320 into
memory, I get an ArgumentException in the Bitmap constructor.
Anything up to 134 works great and once I try to load image 135 it
fails. The code that works fine is:

images = new Bitmap[photos.Count];
for (int i=0;i<134;i++)
{
images = new Bitmap(photos.fileName);
}

Once i change the 134 to anything greater than 134, I get the
exception. I find this very strange that is works fine up to a
certain number. When I look at Memory, I still have 20MB free even
after loading 134 images. Is there some sort of lock that is
preventing me from loading more images? Any help be greatly
appreciated. Thanks so much.

-Tony
 
A

Alex Feinman [MVP]

You might be running out of GDI memory which is a small subset of the
overall system memory. Consider loading these bitmaps on demand
 
B

Bill Draper

Tony,

Just a quick math exercise shows (assuming 2-bytes per pixel color, or 16
bit color)

240 x 2 x 320 = 153,600 bytes per image. 153,600 x 134 images = 20,582,400
bytes, or approx 20 MB!

When you look at memory, I assume you mean system RAM. Windows CE gives each
process a 32 MB 'process space' to run in, and I've always assumed that the
Compact Framework CLR is bound by this 32 MB box (empirical tests show this
to be the case.) That leaves about 12 MB left in your process space. I
assume the CLR itself will occupy part of this space, along with your
managed code and cached JIT'ed code (which I THINK goes into the managed
heap.) There may also be other things that are taking up space from your
process.

Much of this is conjecture on my part, and more info would be needed to fill
in all the pieces (such as what you're using to determine 20 MB free, and
just a more thorough understanding of how CE memory mgt and the CLR work
under the hood) but it's not hard to imagine that you're running out of
memory in your process space using this technique.

Consider whether you can load them on demand and purge when no longer
needed.

Bill
 
B

Bill

You might try the following and see if it works or at least increases
the number of bitmaps you can load...

Change your code as follows:

1) Embed the graphic files in you project as a resource (e.g. click on
the file name and change the "build action" property from "content" to
"embedded resource" (I'm assuming these files are inlcuded in the
project as content files)

2) Change you code to mimic the code that follows:

images = new Bitmap[200];
for (int i=0;i<134;i++)
{
images = setbm(fname);
}


private Bitmap setbm()
{
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(Form1)); //assumes pictures
are loaded at design time into picture boxes on form1)

return (System.Drawing.Bitmap)((System.Drawing.Image)(resources.GetObject(fname)));
}


Bill
 
J

Joe Audette

Tony,

I don't think its a memory issue. I was getting similar
ArgumentException errors and it turned out that the
bitmaps I was trying to load were 32 bit resolution. To
fix it I had to save the bitmaps as 24 bit resolution.

I would bet that the 135th image is more than 24 bit
resolution.

Best Regards,

Joe Audette
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top