Bitmap and Image constructor dependencies (814675)
The information in this article applies to:
- Microsoft GDI+ 1.0
- Microsoft .NET Framework SDK 1.0
- Microsoft .NET Framework SDK 1.0 SP1
- Microsoft .NET Framework SDK 1.0 SP2
- Microsoft Windows XP Professional
- the operating system: Microsoft Windows XP 64-Bit Edition
SYMPTOMSWhen 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 image.
- Create an indexed image.
In both cases, calling the Bitmap.Dispose() method on the original Bitmap removes the lock on the file or removes the requirement that the stream or memory stay alive.
Create a Non-Indexed Image This 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: - Construct the original Bitmap from the stream, from the memory, or from the file.
- Create a new Bitmap of the same size, with a pixel format of more than 8 bits-per-pixel (BPP).
- Use the Graphics.FromImage() method to obtain a Graphics object for the second Bitmap.
- Use Graphics.DrawImage() to draw the first Bitmap onto the second Bitmap.
- Use Graphics.Dispose() to dispose of the Graphics.
- Use Bitmap.Dispose() to dispose of the first Bitmap.
Create an Indexed ImageThis workaround creates a Bitmap object in an indexed format: - Construct the original Bitmap from the stream, from the memory, or from the file.
- Create a new Bitmap with the same size and pixel format as the first Bitmap.
- Use the Bitmap.LockBits() method to lock the whole image for both Bitmap objects in their native pixel format.
- Use either the Marshal.Copy function or another memory copying function to copy the image bits from the first Bitmap to the second Bitmap.
- Use the Bitmap.UnlockBits() method to unlock both Bitmap objects.
- Use Bitmap.Dispose() to dispose of the first Bitmap.
STATUS This
behavior is by design.
Modification Type: | Major | Last Reviewed: | 4/21/2006 |
---|
Keywords: | kbDSWGDI2003Swept kbprb kbgdipimaging kbBitmap KB814675 kbAudDeveloper |
---|
|