title: Disposing of image when using PictureBox
body: <p>I'm working with a Winforms program that was written a while ago. I've come across some issues with it and I'm trying to optimize the way it handles some things but I'm running across some issues with disposing. </p>
  
  <p>Below is what is currently implementing. </p>
  
  <p><strong>First</strong>, It starts with going through the files in the Pictures folder and copies them into a preview folder.</p>
  
  <pre><code>foreach (string s in files)
  {
      fileName = System.IO.Path.GetFileName(s);
      destFile = System.IO.Path.Combine(path, fileName);
      File.Copy(s, destFile, true);
  }
  </code></pre>
  
  <p><strong>Next</strong>, it opens a form via ShowDialog:</p>
  
  <pre><code>frmPreview frm = new frmPreview(FileNameArray, lblParcel.Text);
  frm.ShowDialog();
  </code></pre>
  
  <p><strong>Next</strong>, it goes to the Preview form and gets to this code:</p>
  
  <pre><code>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
  {
  }
  </code></pre>
  
  <p>The code above basically takes all the photos, creates a PictureBox with each one, and adds the PictureBox to the FlowLayoutPanel to display in a window for previewing. Problem with this is that it does not dispose properly and gets caught up after trying to visit this preview window for the 3rd time (closing the window and opening it a second time works fine but creates a second process).</p>
  
  <p><strong>Lastly</strong>, It implements the following when the form closes.</p>
  
  <pre><code>private void frmPreview_FormClosed(object sender, FormClosedEventArgs e)
  {
      this.Dispose();
      this.Close();
  }
  </code></pre>
  
  <p>The error happens on the 3rd time the preview window is called, when the program goes through the <code>foreach</code> statement posted at the top. </p>
  
  <p>The full line where it catches is:</p>
  
  <pre><code>File.Copy(s, destFile, true);
  </code></pre>
  
  <blockquote>
    <p>The process cannot access the file 'C:\Users\username\Pictures\Preview\image.jpg' because it is being used by another process.</p>
  </blockquote>
  
  <p>I'm 99.9% sure it's because of the <code>PictureBox</code> and <code>FlowLayoutPanel</code> but I can't figure out what to do to fix it. I'd like to change as little as possible since this isn't my program and it will soon be rewritten completely. I mainly just need to temporarily fix the issue until we finish the big picture where this whole program will get scrapped.</p>
  
  <p>I've found a couple posts that seem to be similar issues but none of the fixes have changed anything at all. Below are all the posts I looked into and trying implementing unsuccessfully:</p>
  
  <p><a href="https://stackoverflow.com/questions/25468393/file-copy-the-process-cannot-access-the-file-filename-because-it-is-being-use">file-copy-the-process-cannot-access-the-file</a></p>
  
  <p><a href="https://stackoverflow.com/questions/18074839/file-is-being-used-by-another-process-after-file-copy">file-is-being-used-by-another-process</a></p>
  
  <p><a href="https://stackoverflow.com/questions/39439942/dispose-of-a-picturebox-image-without-losing-the-picturebox">dispose-of-a-picturebox</a></p>