Article ID: 814675 - Last Review: April 21, 2006 - Revision: 3.2 Bitmap and Image constructor dependenciesOn This PageSYMPTOMSWhen either a Bitmap object or an Image object is constructed from a file, the file remains locked for
the lifetime of the object. As a result, you cannot change an image and save it
back to the same file where it originated. Additionally, if the stream was destroyed during the life of the Bitmap object, you cannot successfully access an image that was based on a stream. For example, the Graphics.DrawImage() function may not succeed after the stream has been destroyed. CAUSEGDI+, and therefore the System.Drawing namespace, may defer the decoding of raw image bits until the bits are required by the image.
Additionally, even after the image has been decoded, GDI+ may determine that it is more
efficient to discard the memory for a large Bitmap and to re-decode later.
Therefore, GDI+ must have access to the source bits for the image for the life
of the Bitmap or the Image object. To retain access to the source bits, GDI+ locks any source file, and forces the application to maintain the life of any source stream, for the life of the Bitmap or the Image object. WORKAROUNDTo work around this problem, create new Bitmap images by using one of the following methods (as described later in this section):
Create a Non-Indexed ImageThis approach requires that the new image be in a non-indexed pixel format (more than 8 bits-per-pixel), even if the original image was in an indexed format. This workaround uses the Graphics.DrawImage() method to copy the image to a new Bitmap object:
Create an Indexed ImageThis workaround creates a Bitmap object in an indexed format:
STATUS This
behavior is by design. MORE INFORMATIONBy using the Bitmap class constructor and the Image class constructor, you can create
a Bitmap or an Image object that is based on the contents of a file or a stream. Also,
a Bitmap object may be created from a block of memory or from a Windows GDI HBITMAP
handle. GDI+ requires access to the data that is contained in the file or stream or memory block during the life of the new Bitmap or the new Image object. This requirement is because GDI+ does not immediately decode the image bits. Instead, GDI+ delays the decoding of the image bits until the first time that the image is accessed. Additionally, GDI+ may sometimes choose to discard its buffer for the image and re-decode later. To make these types of delayed decoding easier, GDI+ must have access to the raw image data during the life of the object. This can result in some unexpected consequences:
311221
(http://support.microsoft.com/kb/311221/EN-US/
)
INFO: Interoperability between GDI and GDI+
| Article Translations
|
Back to the top
