> Isn't the data read in to the Bitmap object? The Bitmap object does have
> the
[quoted text clipped - 8 lines]
> memory, I need to know until what point I have to keep the Stream open
> too.
No Peter is right, the Bitmap or Image object is constructed from the Stream
(the file or whatever), and you must keep the Stream open for the lifetime
of the Bitmap (or Image).
The reason for this is that GDI+ (System.Drawing) must have access to the
source bits for the Image for the lifetime of the Bitmap or the Image
object, the source may not be modified during that time, that's why the file
is kept locked for the lifetime of the Bitmap or Image.
A possible work-arround is to create a new Bitmap from the first and Dispose
the first when done, when calling Dispose on the Bitmap the File stream will
automatically be closed.
Creating a new Bitmap from an existing Bitmap can be done in two ways:
1. Create an Non-Indexed Image. Here you use Graphics.FromImage and
Graphics.drawImage to copy a Bitmap to anther one.
2. Create an Indexed Image, this uses Blimap.LockBits to lock the pixel data
of both images and marshal.Copy to copy bitmapdata from one to another
Bitmap object bitmapdata.
Here is a sample of the latter...
Bitmap im1 = new Bitmap(@"xxxxx.bmp", true);
Bitmap im2 = new Bitmap(im1, im1.Size);
Rectangle rect = new Rectangle(0, 0, im1.Width, im1.Height);
BitmapData im1Data = im1.LockBits(rect, ImageLockMode.ReadWrite,
im1.PixelFormat);
BitmapData im2Data = im2.LockBits(rect, ImageLockMode.ReadWrite,
im2.PixelFormat);
// Get the address of the first pixel data line.
IntPtr im1ptr = im1Data.Scan0;
IntPtr im2ptr = im2Data.Scan0;
// Declare an array to hold the bytes of the bitmap.
// This code is for a bitmap with 24 bits per pixels bmp
int bytes = im1.Width * im1.Height * 3;
byte[] rgbValues = new byte[bytes];
Marshal.Copy(im1ptr, rgbValues, 0, bytes);
Marshal.Copy(rgbValues, 0, im2ptr, bytes);
// Unlock the bits.
im1.UnlockBits(im1Data);
im2.UnlockBits(im2Data);
im1.Dispose();
// Now the Filestream if unlocked (Closed), you can do whatever you want
with the second bitmap f.i overwrite the sfile holding the first image.
Willy.
David Thielen - 10 Nov 2005 23:01 GMT
Hi;
Ok, I guess I will create a class that holds a bitmap and stream and just
close them as a pair.

Signature
thanks - dave
> > Isn't the data read in to the Bitmap object? The Bitmap object does have
> > the
[quoted text clipped - 53 lines]
>
> Willy.