tags:

views:

145

answers:

2

First sorry for the english. I'm having an strange problem when I'm trying to save a bitmap ("Generic GDI+ error") from a picturebox in my win app, I know that this problem occurs when trying to save to a directory that has no permission, but in this case I'm trying to save it to a memoryStream.

using (MemoryStream fotoStream = new MemoryStream())
{
    Bitmap imagen = picture1.Image as Bitmap;
    imagen.Save(fotoStream, imagen.RawFormat); //Error here
    byte[] array = fotoStream.ToArray();
    fotoStream.Close();
}

Any light on this?

A: 

Can you save the image in any other format, or by calling the Save overload that doesn't specify a format?

Have you checked the dimensions of the image to ensure that both width and height are non-zero?

Adam Robinson
I can't use an overload for saving the image because the one I'm using is the only one that accepts a stream.
Argons
@Argons: Have you tried using a different format?
Adam Robinson
A: 

How are you saving it to the database? You might want to create a test program that compares what you sent to the database and what you get back to make sure that they are the same.

Can you give us the full stack trace of the exception? imagen can't be null in this case or the exception would be a null exception when trying to access .RawFormat.

What does imagen look like in the debugger?

The full stack trace: en System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams) en System.Drawing.Image.Save(Stream stream, ImageFormat format) en SPCC.Win.Seguridad.frmUsuarios.get_Foto() en SPCC.MVP.Seguridad.Presenters.UsuariosPresenter.GrabarUsuario() ..................But I think is something related with the RawFormat, because I'm loading the image from the database. Has anyone had the same problem?
Argons
I'm using NHibernate to save the data, it's saving and loading the image correctly, when I load the image from the database it's displayed on a picturebox correctly, the problem is when I try to save the same image loaded from the DB. The error is not raised if I load another image into the picturebox.
Argons