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:
C#
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.
Ex.
C#
Image foreImage = Image.FromFile(tbxForeImage.Text);
foreImage.Dispose();


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

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.

—SA
 
Share this answer
 
Comments
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...
--SA
if(pictureBox1.Image!=null)
pictureBox1.Image.Dispose();

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