Click here to Skip to main content
15,882,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
pictureBox1.Image = Image.FromFile(tbxImageName.Text);

I get an image from above code.
I want to close all the reference related to the above code.
Image foreImage = Image.FromFile(tbxForeImage.Text);

If anybody knows please help me. Thank You very much
Updated 18-Aug-11 20:15pm

Don't call Dispose as Suresh advised — this code does not take into account possible exceptions.

Do this:
(using Image foreImage = Image.FromFile(tbxForeImage.Text)) {
    //use foreImage here
} //foreImage.Dispose() will be called here even it exception is thrown

You should to it to all temporary objects implementing System.IDisposable.

Only if the object is used during the life time of the declaring class, dispose in the Dispose of the owner class, hierarchically. For a good example, look at System.Windows.Forms.Form.Dispose (which is not System.IDisposable.Dispose) and usage samples.

Share this answer
Suresh Suthar 19-Aug-11 2:33am    
Thanks for this answere SA. I just delete my solution which was wrong. Have my 5.
Sergey Alexandrovich Kryukov 19-Aug-11 12:33pm    
Thank you very much, Suresh. Not a big deal, we all do mistakes sometimes...

pictureBox1.InitialImage = null;
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900