在循环中重新创建一个大位图之前是否需要Dispose()?

时间:2017-02-23 14:31:23

标签: c#

假设我有代码:

void Method1() {
    Bitmap bitmap1;
    foreach (string name in OpenFileDialog1.FileNames) {
        bitmap1 = new Bitmap(name);
        ... // process bitmap
        bitmap1.Dispose();
    }
}

循环中是否需要Dispose()?

1 个答案:

答案 0 :(得分:0)

噢,是的。该位图对象正在文件上打开一个内存映射!

更好的方法,因为此代码结构可以使用using

void Method1() {
    foreach (string name in OpenFileDialog1.FileNames) {
        using (var bitmap1 = new Bitmap(name))
        {
            ... // process bitmap
        }
    }
}
相关问题