0

I have a form with OpenFileDialog for selecting image and showing it in pictureBox. Until the form is open the user can open and then save the opened image as many times as he wants. What I want to do is, after each new selection-save, to delete the previously saved image if there is such. The problem is that as I implemented the code now I am able to delete the image the first time, if I keep on saving images with the currently open form I get an error that the resource is being used. What I do is Dispose() the image but I guess I don't do it in the right place.

This is how I open and load the image:

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);
                }
            }
        }

And in the same class I have this method which I call if I need to delete an old image:

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);
        }

As you can see. The Dispose() method is here which I though will ensure that the resource will be disposed before trying to delete it but as I said this only work once and then I get the error as many times as attempts to save image I make.

P.S

The exact error I get is:

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

3 に答える 3

5

それを行うためのより良い方法があります。FileStreamを使用して画像をロードし、それをpictureBoxに割り当てます

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を使用する方が良いです...)

「破棄」とは、ガベージコレクターに未使用のオブジェクトをメモリから強制的に削除させることです。

エラーは、pictureBox-imageの破棄とは関係ありませんが、ソースファイルのロックと関係があります。

openFileDialogの処理に「using」ブロックを使用すると、すでに役立つかもしれません。

于 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 に答える