C# Image Array in Exception

  • Thread starter Thread starter mcm
  • Start date Start date
M

mcm

I need to do an animation of several run-time generated drawings.

The drawings are approximately 200x400

I'm trying to do it in this manner, however I keep getting OutOFMemoryException's when I try to create the images with the "new Bitmap" call.
I don't think that these images are all that big - are they? How can I do this without the exceptions in C# compact framework?


Example code
{

Image[] myImages = new Image[36];

for (int i=0; i<36; i++)
{
Rectangle rectDraw ;
rectDraw.X = 0;
rectDraw.Y = 0;
rectDraw.Width = 200;
rectDraw.Height = 400

myImages = DrawTheImage(rectDraw) ;

}
}


public Image DrawTheImage (Rectangle rectDraw)
{
Image thisImage = new Bitmap(rectDraw.Width, rectDraw.Height); // CRASH after about 20 calls
Graphics g = Graphics.FromImage(thisImage);

g.DrawMyStuffHere .............

g.Dispose();

return thisImage;
}
 
Thanks Chris,

I had seen your blog posting on the topic and was going to try to create an image file that was 200x400 and then use the: new Bitmap(fileStream) approach.
However, it was getting late, so I threw up this question to see if there was any other knowledge out there. Would a DIB be that much smaller?

My dillemma is that I was able to this with MFC on the same system. Now that I'm porting to C# I seem to have crashed into a memory wall. Could this be the DIB vs. DDB problem?

Regarding the disposing of "thisImage", how can I dispose of it when it is being used as the return value?
I guess one option would be to pass myImages as an argument to DrawTheImage
then I wouldn't have to dispose of anything.

- Mark






200px by 400 px at 16bpp = 160k per image
200px by 400 px at 24bpp = 240k per image

36 images = 5.76MB or 8.64MB depending on color depth.

So that's a fair bit just out of the gate.

You're also never Disposing the thisImage instance that I see you creating
in DrawTheImage. The CF has an issue where this can be a problem:

http://blog.opennetcf.org/ctacke/PermaLink,guid,987041fc-2e13-4bab-930a-f79021225b74.aspx


--

Chris Tacke, eMVP
Join the Embedded Developer Community
http://community.opennetcf.com



mcm said:
I need to do an animation of several run-time generated drawings.

The drawings are approximately 200x400

I'm trying to do it in this manner, however I keep getting
OutOFMemoryException's when I try to create the images with the "new
Bitmap" call.
I don't think that these images are all that big - are they? How can I do
this without the exceptions in C# compact framework?


Example code
{

Image[] myImages = new Image[36];

for (int i=0; i<36; i++)
{
Rectangle rectDraw ;
rectDraw.X = 0;
rectDraw.Y = 0;
rectDraw.Width = 200;
rectDraw.Height = 400

myImages = DrawTheImage(rectDraw) ;

}
}


public Image DrawTheImage (Rectangle rectDraw)
{
Image thisImage = new Bitmap(rectDraw.Width, rectDraw.Height); //
CRASH after about 20 calls
Graphics g = Graphics.FromImage(thisImage);

g.DrawMyStuffHere .............

g.Dispose();

return thisImage;
}

 
I had seen your blog posting on the topic and was going to try to create
an image file that was 200x400 and then use the: new Bitmap(fileStream)
approach.
However, it was getting late, so I threw up this question to see if there
was any other knowledge out there. Would a DIB be that much smaller?

DIB v DDB won't be much different. It might be 16 v 32 bpp, but ti's still
going to take a lot of memory because you're loading 36 bitmaps in memory at
one time. Wouldn't be any different in native code - they also have to
expand a bitmap to show it. If you were loading all 36 at once, then they
took just as much RAM.
My dillemma is that I was able to this with MFC on the same system. Now
that I'm porting to C# I seem to have crashed into a memory wall. Could
this be the DIB vs. DDB problem?
Doubtful.

Regarding the disposing of "thisImage", how can I dispose of it when it is
being used as the return value?
I guess one option would be to pass myImages as an argument to
DrawTheImage
then I wouldn't have to dispose of anything.


I wouldn't load all 36 images. You don't need them all. I'd play with a
circular cache of maybe 6 or so and demand load the images up to 5 ahead of
where you're showing. The number actually needed would depend on testing to
see how fast they can be loaded versus how fast you need to show.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com
 
Back
Top