Re: Error Saving Bitmaps when stored in an array?

Tech-Archive recommends: Speed Up your PC by fixing your registry



"shp" <shp@xxxxxxxxxxxxx> wrote in message
news:D2DA339C-3F24-4E89-AEFD-47916BD12929@xxxxxxxxxxxxxxxx
Hi,

I have an array of Bitmap images which I am trying to Serialize. I was
having a problem Serializing the Bitmaps directly (Although I'm not sure
why,
the documentation seems to indicate it is Serializable?) but when I try to
save the Bitmaps from the array structure an error is thrown.

I.e. If I set it up like this:

MemoryStream ms = new MemoryStream();
bmpFrames[0].Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);

During runtime I receive the following error:
"A generic error occurred in GDI+."

You will get this error if the *source* of the bitmap has been closed. eg,
if the bitmap was created from a stream and the stream was closed then you
will get that error when saving the bitmap. You can either keep the original
source open, which is a complete pain or create a copy right from the start,
something like this:

Stream stream = GetSomeStream();
Bitmap bitmap = new Bitmap(stream);
Bitmap bitmap2 = new Bitmap(bitmap);
bitmap.Dispose();
stream.Close();
return bitmap2;

For cloning the bitmap I use LockBits with a memcopy, this ensures that
bitmap2 is completely independant of bitmap.

Michael


.



Relevant Pages

  • Re: Speed of C# and GDI+
    ... .NET CF compiler generates MSIL code which is a CPU independent ... However the Bitmap ... class provides a constructor which takes a stream parameter. ... with this array as the parameter. ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Speed of C# and GDI+
    ... .NET CF compiler generates MSIL code which is a CPU independent ... However the Bitmap ... class provides a constructor which takes a stream parameter. ... with this array as the parameter. ...
    (microsoft.public.pocketpc.developer)
  • Re: overlay text on uploaded picture
    ... If it is then all I need to do is stream my original image ... The reason I was creating a second bitmap is that I need the image in the ... form of a Byte array. ... I couldn't go direct from a Graphics object to a Byte array so I ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Serializing Bitmap
    ... private Bitmap _img; ... public Bitmap Img ... must convert it to a stream - serialize? ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: System.Drawing.Image and jpg images
    ... > then like to read the data and reconstruct the jpg. ... > into a byte array, and this is where I'm stumped. ... I assume it's expecting a bitmap. ... > stream the bytes to a disk file and then read it back in, ...
    (microsoft.public.dotnet.framework)