Bitmaps from streams - duplicated data in memory?

  • Thread starter Thread starter Chris Ashley
  • Start date Start date
C

Chris Ashley

Hi,

If I create a Bitmap object from a stream, is the data making up the
image from that stream duplicated in the Bitmap object, or does the
Bitmap just point to the stream somehow without holding any image
data?

I did notice that if I populated a Bitmap object from a MemoryStream,
if I closed the MemoryStream I got a GDI error when I tried to save
the Bitmap, so I assumed it was the latter. Just looking for
confirmation.

Thanks,

Chris
 
Chris said:
If I create a Bitmap object from a stream, is the data making up the
image from that stream duplicated in the Bitmap object or does the Bitmap
just point to the stream somehow without holding any image data?

It depends on whether the stream is seekable. If it is not, the data will be
copied. If it is, it will use the stream directly.

That's an implementation detail, though. The documentation for Bitmap
explicitly states: "You must keep the stream open for the lifetime of the
Bitmap." Bitmap could one day read lazily from non-seekable streams if it so
desired.
I did notice that if I populated a Bitmap object from a MemoryStream,
if I closed the MemoryStream I got a GDI error when I tried to save
the Bitmap, so I assumed it was the latter. Just looking for
confirmation.
Now you know you shouldn't do that, even if it happens to work on some
streams. :-)
 
Back
Top