You cannot delete / close the stream while the raster object is still using it. (Is it possible that the raster image object will have access to it again, will be determinate only if you know what type of file you are working with and what operations you will perform. For example, for .gif images, the stream closed before the constructor returns.)
The clone creates an βexact copyβ of the bitmap (according to the documentation, ILSpy shows that it calls its own methods, so it is too much to track right now), it also copies the Stream data, and also will not, t is an exact copy.
Itβs best to create a perfect copy of the image - although YMMV (with certain types of images there may be more than one frame, or you may have to copy the data from the palette). But for most images, this works:
static Bitmap LoadImage(Stream stream) { Bitmap retval = null; using (Bitmap b = new Bitmap(stream)) { retval = new Bitmap(b.Width, b.Height, b.PixelFormat); using (Graphics g = Graphics.FromImage(retval)) { g.DrawImage(b, Point.Empty); g.Flush(); } } return retval; }
And then you can call it like this:
using (Stream s = ...) { Bitmap x = LoadImage(s); }
BrainSlugs83 Nov 01 '11 at 9:10
source share