Image Dispose

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?
 
H

Herfried K. Wagner [MVP]

* "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.
 
S

steve

Fair enough.

Herfried K. Wagner said:
You must not close the stream the image is created from when you are
keeping using the image constructed from the stream.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top