MSDOTnet.org Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

C# Image Array in Exception

 
Post new topic   Reply to topic    MSDOTnet.org Forum Index -> Compact Framework
Author Message
mcm



Joined: 20 Jan 2008
Posts: 2

PostPosted: Wed Feb 06, 2008 1:44 am    Post subject: C# Image Array in Exception Reply with quote

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[i] = 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;
}

Archived from group: microsoft>public>dotnet>framework>compactframework
Back to top
View user's profile Send private message
user



Joined: 08 Aug 2007
Posts: 286

PostPosted: Wed Feb 06, 2008 2:33 am    Post subject: Re: C# Image Array in Exception Reply with quote

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" wrote in message
news:%23qIMJHGaIHA.4696@TK2MSFTNGP05.phx.gbl...
>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[i] = 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;
> }
>
>
>
>
Back to top
View user's profile Send private message
mcm



Joined: 20 Jan 2008
Posts: 2

PostPosted: Wed Feb 06, 2008 12:13 pm    Post subject: Re: C# Image Array in Exception Reply with quote

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[i] 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" wrote in message
>news:%23qIMJHGaIHA.4696@TK2MSFTNGP05.phx.gbl...
>>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[i] = 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;
>> }
>>
>>
>>
>>
>
Back to top
View user's profile Send private message
user



Joined: 08 Aug 2007
Posts: 286

PostPosted: Wed Feb 06, 2008 1:08 pm    Post subject: Re: C# Image Array in Exception Reply with quote

> 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[i] 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 to top
View user's profile Send private message
Display posts from previous:   
Related Topics:
How to rotate image? How to rotate image in .net cf? I want to get the original image's scan line, than create a new bitmap and get its scan line and write to it directly. My question is how to get the scan line? And I also wounder what is The return of it

How to put a image to the menu item? The MenuItem class in CF dose not support OwnerDraw and Handle property. I can not get the form's native handle also. I have no idea of putting a image to the menu item other than use OwerDraw. Anyone can give me some comment? Thanks! Dolphin White

Resizing a bitmap image that works? Hi all, Does anyone know how to resize a bmp image on the compact Framework? I have spent ages trying and looking for an answer. Any help will be appriciated. I have some code I've been trying to to work:- Bitmap b1 = new Bitmap newI

Call Image.Dispose when clearing an Image List? Hi, I have a listview which is set to view items in a small icon view. It's set up to get the images from an image list. When I populate the listview, I clear the image list and add new images for each item. This seems to work fine, but what I was wonderi

WMI - Exception ....HELP! Can someone explain this error ? I am using Framework 1.1, Visual Studio and Win XP. "COM object that has been separated from its underlying RCW can not be used." Here is the code I am calling try { ObjectQuery query = new
Post new topic   Reply to topic    MSDOTnet.org Forum Index -> Compact Framework All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group