Deleting File which is displayed in picturebox

前端 未结 5 1291
北海茫月
北海茫月 2021-01-17 15:39

I am selecting file from openfiledialoge and displaying it in picturebox and its name in textbox when I click on delete button I am getting exception The

5条回答
  •  甜味超标
    2021-01-17 15:57

    The (previously) accepted answer to this question is very poor practice. If you read the documentation on System.Drawing.Bitmap, in particular for the overload that creates a bitmap from a file, you will find :

    The file remains locked until the Bitmap is disposed.

    in your code you create the bitmap and store it in a local variable but you never dispose of it when you are done. This means your image object has gone out of scope but has not released its lock on the image file you are trying to delete. For all objects that implement IDisposable (like Bitmap) you must dispose of them yourself. See this question for example (or search for others - this is a very important concept!).

    To correct the problem properly you simply need to dispose of the image when you are done with it :

     if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
     {
          Image img = new Bitmap(ofd.FileName);  // create the bitmap
          string imgName = ofd.SafeFileName;
          txtImageName.Text = imgName;
          pictureBox1.Image = img.GetThumbnailImage(350, 350, null, new IntPtr());
          ofd.RestoreDirectory = true;
          img.Dispose();  // dispose the bitmap object
     }
    

    Please do not take the advice in the answer below - you should nearly never need to call GC.Collect and if you need to do it to make things work it should be a very strong signal that you are doing something else wrong.

    Also, if you only want to delete the one file (the bitmap you have displayed) your deletion code is wrong and will delete every file in the directory as well (this is just repeating Adel's point). Further, rather than keep a global OpenFileDialog object alive simply to store the file name, I would suggest getting rid of that and saving just the file info :

    FileInfo imageFileinfo;           //add this
    //OpenFileDialog ofd = null;      Get rid of this
    
    private void button1_Click(object sender, EventArgs e)
    {
         if (System.IO.Directory.Exists(path))
         {
             OpenFileDialog ofd = new OpenFileDialog();  //make ofd local
             ofd.InitialDirectory = path;
             DialogResult dr = new DialogResult();
             if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
             {
                  Image img = new Bitmap(ofd.FileName);
                  imageFileinfo = new FileInfo(ofd.FileName);  // save the file name
                  string imgName = ofd.SafeFileName;
                  txtImageName.Text = imgName;
                  pictureBox1.Image = img.GetThumbnailImage(350, 350, null, new IntPtr());
                  ofd.RestoreDirectory = true;
                  img.Dispose();
             }
             ofd.Dispose();  //don't forget to dispose it!
         }
         else
         {
             return;
         }
     }
    

    Then in your second button handler you can just delete the one file you are interested in.

            private void button2_Click(object sender, EventArgs e)
            {                
               if (!IsFileLocked(imageFileinfo))
                {                 
                    imageFileinfo.Delete();
                }
            }
    

提交回复
热议问题