使用 PictureBox 时处理图像

Disposing of image when using PictureBox

我正在使用不久前编写的 Winforms 程序。我遇到了一些问题,我正在尝试优化它处理某些事情的方式,但我 运行 遇到了一些处理问题。

以下是目前正在实施的内容。

首先,它首先浏览图片文件夹中的文件并将它们复制到预览文件夹中。

foreach (string s in files)
{
    fileName = System.IO.Path.GetFileName(s);
    destFile = System.IO.Path.Combine(path, fileName);
    File.Copy(s, destFile, true);
}

下一步,它通过ShowDialog打开一个表单:

frmPreview frm = new frmPreview(FileNameArray, lblParcel.Text);
frm.ShowDialog();

下一步,它进入预览表单并获取此代码:

try {
    FlowLayoutPanel imagePanel = new FlowLayoutPanel();
    if (System.IO.Directory.Exists(path))
    {
        folder = new DirectoryInfo(path);

        foreach (FileInfo files in folder.GetFiles())
        {
            System.Diagnostics.Debug.Print(files.Extension);
            if ((string.Equals(files.Extension, ".jpg", StringComparison.OrdinalIgnoreCase)) || (string.Equals(files.Extension, ".gif", StringComparison.OrdinalIgnoreCase)) || (string.Equals(files.Extension, ".png", StringComparison.OrdinalIgnoreCase)))
            {
                PictureBox image = new PictureBox();
                image.Image = Image.FromFile(files.FullName);
                image.SizeMode = PictureBoxSizeMode.Zoom;
                image.Size = this.Size;
                imagePanel.Controls.Add(image);
            }
        }
    }
    this.Controls.Add(imagePanel);
    System.Threading.Thread.Sleep(0);

    return;
}
catch
{
}

上面的代码基本上是获取所有照片,为每张照片创建一个 PictureBox,并将 PictureBox 添加到 FlowLayoutPanel 中以显示在 window 中以供预览。问题是它没有正确处理并在第三次尝试访问此预览 window 后被卡住(关闭 window 并再次打开它工作正常但创建了第二个进程).

最后,它在窗体关闭时执行以下操作。

private void frmPreview_FormClosed(object sender, FormClosedEventArgs e)
{
    this.Dispose();
    this.Close();
}

错误发生在第 3 次调用预览 window 时,当程序执行顶部发布的 foreach 语句时。

它捕获的完整行是:

File.Copy(s, destFile, true);

The process cannot access the file 'C:\Users\username\Pictures\Preview\image.jpg' because it is being used by another process.

我 99.9% 确定这是因为 PictureBoxFlowLayoutPanel 但我不知道该怎么做才能修复它。我想尽可能少地改变,因为这不是我的程序,它很快就会被完全重写。我主要只是需要暂时解决这个问题,直到我们完成整个程序将被废弃的大局。

我发现了一些似乎是类似问题的帖子,但 none 的修复完全改变了任何东西。以下是我查看并尝试实施但未成功的所有帖子:

file-copy-the-process-cannot-access-the-file

file-is-being-used-by-another-process

执行@RezaAghaei 的建议后问题已解决。将预览表单更改为:

foreach (FileInfo files in folder.GetFiles())
{
    System.Diagnostics.Debug.Print(files.Extension);
    if ((string.Equals(files.Extension, ".jpg", StringComparison.OrdinalIgnoreCase)) || (string.Equals(files.Extension, ".gif", StringComparison.OrdinalIgnoreCase)) || (string.Equals(files.Extension, ".png", StringComparison.OrdinalIgnoreCase)))
    {
        using (var stream = new FileStream(files.FullName, FileMode.Open))
        {
            PictureBox image = new PictureBox();
            image.Image = Image.FromStream(stream);
            image.SizeMode = PictureBoxSizeMode.Zoom;
            image.Size = this.Size;
            imagePanel.Controls.Add(image);
        }
    }
}

我还通过实施 using 块提高了 ShowDialog 调用的效率:

using (frmPreviewPhotos frm = new frmPreviewPhotos(NEWphotoFileNameArray, lblParcel.Text))
{
    frm.ShowDialog();
}