0

我有一个表格,OpenFileDialog用于选择图像并将其显示在pictureBox. 在表单打开之前,用户可以根据需要多次打开并保存打开的图像。我想要做的是,在每次新的选择保存后,删除以前保存的图像(如果有的话)。问题是,当我现在实现代码时,我可以第一次删除图像,如果我继续使用当前打开的表单保存图像,我会收到资源正在使用的错误。我所做的是Dispose()图像,但我想我没有在正确的地方做。

这是我打开和加载图像的方式:

private void btnExplorer_Click(object sender, EventArgs e)
        {

            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Title = "Select file";
            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = fileNameFilter;
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.FileName = prefixFilter;


            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    pictureBox1.InitialImage = new Bitmap(openFileDialog1.FileName);
                    pictureBox1.ImageLocation = openFileDialog1.FileName;
                    selectedFile = pictureBox1.ImageLocation;
                    selectedFileName = openFileDialog1.SafeFileName;
                    pictureBox1.Load();
                }
                catch (Exception ex)
                {
                    logger.Error(ex.ToString());
                    MessageBox.Show("Error loading image!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

在同一个课程中,如果我需要删除旧图像,我会调用这个方法:

public void DeleteImage(AppConfig imagePath, string ImageName)
        {
            pictureBox1.InitialImage.Dispose();//Release the image before trying to delete it
            string imgPath = imagePath.ConfigValue.ToString();
            File.Delete(imgPath + "\\" + ImageName);
        }

如你看到的。该Dispose()方法就在这里,尽管我将确保在尝试删除资源之前将其处理掉,但正如我所说,这只工作一次,然后我得到的错误次数与我尝试保存图像的次数一样多。

附言

我得到的确切错误是:

The process cannot access the file 'C:\Images\ME_083a210e1a7644198fe1ecaceb80af52.jpg' because it is being used by another process.
4

3 回答 3

5

有更好的方法来做到这一点。使用FileStream加载图像并将其分配给图片框

FileStream bmp = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
Image img = new Bitmap(bmp);
pictureBox1.Image = img;
bmp.Close();

如果你想清除图片框,只需

pictureBox1.Image = null;
于 2013-03-05T09:03:51.793 回答
1

如果我理解正确,您想从图片框中删除曾经“使用过”的图像:设置

picturebox.InitialImage=null;

(顺便说一句:你最好使用picturebox.Image ...)

“Dispose”是强制垃圾收集器从内存中删除一个未使用的对象。

您的错误与 pictureBox-image 的处理无关,而是与源文件的锁定有关。

如果您使用“使用”块来处理 openFileDialog,也许它已经有所帮助。

于 2013-03-05T09:04:00.940 回答
0
  private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Title = "Select file";
            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "Jpeg Files(*.jpg)|*.jpg|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.InitialImage = new Bitmap(openFileDialog1.FileName);
                pictureBox1.ImageLocation = openFileDialog1.FileName;
                selectedFile = pictureBox1.ImageLocation;
                selectedFileName = openFileDialog1.SafeFileName;
                pictureBox1.Load();

            }


        }

        public string selectedFileName { get; set; }

        public string selectedFile { get; set; }

        private void button2_Click(object sender, EventArgs e)
        {
            pictureBox1.ImageLocation = null;
        }
于 2013-03-05T09:11:28.947 回答