views:

230

answers:

3

Hello.

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!

A: 

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?

Jon Skeet
I've added more details to my question.
VansFannel
You still haven't said how big the image is in terms of pixels instead of bytes. You should also check (e.g. with MD5) that you're correctly grabbing it from the database.
Jon Skeet
I've saved the image and Ican't open it.
VansFannel
A: 

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..

ctacke
I've added more details to my question.
VansFannel
+1  A: 

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.

Henk Holterman