Bitmaps from streams - duplicated data in memory?

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
 
J

Jeroen Mostert

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top