VansFannel
VansFannel

Reputation: 45921

new Bitmap OutOfMemoryException in CF 2.0SP2

I have this code:

if (archivoBinario != null)
{
    MemoryStream ms = new MemoryStream(archivoBinario);
    Bitmap imagen = new Bitmap(ms);
    PicBoxImagen.Image = imagen;
}

It throws a System.OutOfMemoryException when a create a new Bitmap from MemoryStream ms.

Note: archivoBinario is a byte array witch its size is 9778 bytes.

I think the size on memory it's not the problem. Any advice?

The images are sent to the device by a WCF service and stored in a SQL Server CE 3.1 database. Maybe it can occur a problem while sending image.

I have compare the bytes representing the image stored in SQL Server 2005 and the image stored in SQL Server CE and are the same.

Thank you!

Upvotes: 0

Views: 1242

Answers (3)

Henk Holterman
Henk Holterman

Reputation: 273219

The Image class throws OOM for just about anything, including invalid format. To check your bases, Make sure it is a valid Image. Save those 9778 bytes to a file and try to view it (on CF and/or a normal PC).

But it is possible for a 9 kB compressed image to blow up enormously so it still could be a genuine OOM.

Upvotes: 1

ctacke
ctacke

Reputation: 67178

You must call Dispose on PicBoxImagen.Image if it is not null before assigning a new image. If you don't you have a leak. See this blog entry for a more detailed explanation as to why..

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500245

Creating an image will sometimes throw an OutOfMemoryException for resources other than memory (confusingly enough).

Is it possible that you haven't been disposing of Windows Forms handles?

The other possibility is that you really are short on memory - for example, a small file could still represent an enormous picture (e.g. if it's all one colour). If Windows is trying to create an in-memory pixel-by-pixel representation of the image, that could display the same symptoms. What size is the picture in terms of pixels?

Upvotes: 0

Related Questions