Why i should keep stream opened while using Bitmap

Go To StackoverFlow.com

2

I can't figure out why i should keep stream opened while i'm using Bitmap or Image.

I try to close the stream and use my Image several ways and everything fine.

The question is in what cases i can get exception if continue close the stream after initialization Image object?

2012-04-05 19:40
by gingray
Please post your code - dash 2012-04-05 19:42


5

The Image class will not read in the whole image upon creation through either Image.FromFile or Image.FromStream, but instead only read enough to respond to basic queries such as size and pixel format.

Only if you attempt to access the pixels in any way (usually by drawing the image on-screen), does it read the image data.

This lazy nature means that it needs access to the underlying stream (or file) when it needs to later on in its life.

2012-04-05 19:46
by Lasse Vågsæther Karlsen
You can of course read the image data into a memory stream (or perhaps use other apis to load the image immediately) so that the underlying file can be closed. But this is only necessary if it is a problem to leave the file open - Jason Williams 2012-04-05 19:54
Ads