Image Dispose

  • Thread starter Thread starter steve
  • Start date Start date
S

steve

Here's code that works fine. It gets an Image from a file stream, and
raises an event passing the image object, then closes the fileStream.

Image imgObj = Image.FromStream(fStream,true);
// Raise Event SingleImageCaptured and pass Image object data
OnSingleImageCaptured(new RemoteDeviceEventArgs(imgObj ));
fStream.Close();

I'd like to do something like this to Dispose of the Image as soon as it's
done being used.

using(Image imgObj = Image.FromStream(fStream,true))
{
// Raise Event SingleImageCaptured and pass Image object data
OnSingleImageCaptured(new RemoteDeviceEventArgs(copyImage));
fStream.Close();
}

The above code raises an System.Drawing exception "Invalid parameter used."
I realize the Image is probably disposed before it's used in the client who
subscribes to OnSingleImageCaptured(). My goal is to clean up (Dispose) the
Image object resource as soon as I'm done with it rather than let the GC
handle it sometime in the very far future. Is it possible and Dispose of
the Image object and should it really be done at this point?
 
* "steve said:
Here's code that works fine. It gets an Image from a file stream, and
raises an event passing the image object, then closes the fileStream.

You must not close the stream the image is created from when you are
keeping using the image constructed from the stream.
 
Back
Top