与图片框有关 [英] related to picturebox
问题描述
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.
Image foreImage = Image.FromFile(tbxForeImage.Text);
foreImage.Dispose();
如果有人知道,请帮助我.非常感谢您
If anybody knows please help me. Thank You very much
推荐答案
不要像Suresh所建议的那样调用Dispose
-此代码未考虑可能的异常.
请执行以下操作:
Don''t callDispose
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
您应该对实现System.IDisposable
的所有临时对象执行此操作.
仅当在声明类的生存期内使用该对象时,才将其分层放置在所有者类的Dispose
中.举个好例子,看看System.Windows.Forms.Form.Dispose
(不是不是 System.IDisposable.Dispose
)和用法示例.
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.
if(pictureBox1.Image!= null)
pictureBox1.Image.Dispose();
pictureBox1.InitialImage = null;
if(pictureBox1.Image!=null)
pictureBox1.Image.Dispose();
pictureBox1.InitialImage = null;
这篇关于与图片框有关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!