title: Why is PictureBox.Load locking image on some systems?
body: <p>(Please see the edit on the bottom of the question, if you do not want to read the whole story.)</p>
  
  <p>Hi, </p>
  
  <p>I am new to stackoverflow. Don’t get me wrong, I use it quite often. But up until now I never actually posted something. This is because I did not have something new/useful to say and my English is not that good. The first thing (might have) changed, the latter did not.</p>
  
  <p><strong>I ran into a problem at a customer's Windows 7 system quite recently. I was shipping a C# .Net 4.0 Windows Forms application via ClickOnce. Basically, it is an application that creates a bitmap file and shows it to the user. If the bitmap exists prior to the creation, the existing file gets deleted first. After that the new file is created and loaded by a PictureBox.</strong></p>
  
  <p>The following thing occurred at the customer’s system: After starting the application the first creation succeeds – the second and all following ones do not. The file cannot be deleted, because some process is blocking it. This process is the application itself.</p>
  
  <blockquote>
    <p>System.IO.IOException: The process cannot access the file “filename” because it is being used by another process.</p>
  </blockquote>
  
  <p>Well, of course that is nothing unusual. The thing is I tested the application on several systems. None showed this exception. And until now I am unable to see an code error.</p>
  
  <p><strong>So I looked a little bit closer on the customer’s system: The only difference I could find is, that they changed the users folder so that they are not located on the windows partition, but on a different one (C:\Users --> D:\Users). I searched for an instruction on the internet and did the same thing on one of my test systems. To my surprise I got the same exception when I ran my application on it.</strong></p>
  
  <p>With that I could change my code so that the exception does not occur anymore. But I do not understand why that is. So maybe there is something wrong with my code and the error just reveals itself under that special circumstances. Or maybe the code is okay and the reason lies somewhere else. I just hoped that you might be able to help me.</p>
  
  <p>So here is some code I put together, that shows the same behavior. I used 3 buttons, an OpenFileDialog and a PictureBox on a Form. First thing to do is to choose an image file. By pressing one of the two remaining buttons it gets copied into the main folder of the application. After being copied it is shown by the PictureBox. By the way, it does not seem to matter if it is a ClickOnce-application or a “normal” one.</p>
  
  <pre><code>String m_FileName;
  
  private void btnChooseFile_Click(object sender, EventArgs e) {
      if(openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { // If file was chosen, set file name for later use and activate buttons.
          m_FileName = "Test" + Path.GetExtension(openFileDialog1.FileName);
      }
  }
  
  private void button1_Click(object sender, EventArgs e) {
  
      // This is not working.
      if(this.pictureBox1.Image != null) {
          //Image img = this.pictureBox1.Image; // I was not sure, if maybe the pictureBox somehow prevents the disposing of the image, as long as it's assigned to it.
          //this.pictureBox1.ImageLocation = null; // So I set them null, both the image and the image location.
          //this.pictureBox1.Image = null; 
          //img.Dispose(); // Then I disposed the image.
  
          this.pictureBox1.Image.Dispose(); // The short version. It is not working either way.
          this.pictureBox1.Image = null;
      }
      (new FileInfo(openFileDialog1.FileName)).CopyTo(m_FileName, true); // But still this is where the Exception occurs.
      this.pictureBox1.Load(m_FileName);
  }
  
  private void button2_Click(object sender, EventArgs e) {
  
      //This is working.
      if(this.pictureBox1.Image != null) {
          //Image img = this.pictureBox1.Image;
          //this.pictureBox1.Image = null;
          //img.Dispose();
  
          this.pictureBox1.Image.Dispose();
          this.pictureBox1.Image = null;
      }
      (new FileInfo(openFileDialog1.FileName)).CopyTo(m_FileName, true);
      pictureBox1.Image = Image.FromFile(m_FileName);
  }
  </code></pre>
  
  <p>What happens now is the following: If I start the application and click button1 twice, I will get the exception (on the second click). If I start it and click button2 twice, I will not. If I start the application and click buttons1 first and after that button2, I will get the exception. So, the Picture.Load-Function somehow blocks the file, even if I dispose it.</p>
  
  <p>When I searched on the internet, I found an article from microsoft: <a href="http://support.microsoft.com/kb/309482/en-us" rel="nofollow">http://support.microsoft.com/kb/309482/en-us</a>. But it does not hit the bull's eye.</p>
  
  <p><strong>Take into account, that both versions are working on all my test machines. I just get the exception when I change the users folder to a non-windows-partition.</strong></p>
  
  <p>Why is that?  And where is the difference in the presented versions?</p>
  
  <hr>
  
  <p><strong>Edit</strong></p>
  
  <p>Okay, because of the first and only reaction so far, it seems to me, that it is still not clear, what really happens: If I take the above code, put it in a Windows Forms application, compile it and run it on different computers (at work, at home, does not matter) it works - both button1 and button2 (with the Click-functions linked to them) can be used as often as I like - no exception thrown. If I run the application on a computer, where I changed the users folder, and click button1 the second time - bam - IOException, file locked by process. Button2 works as long as I do not press button1.</p>
  
  <p>The first answer implies, that I should get the locking on every system. But I DO NOT (as long as I do not change the users folder)! I tested it on every single computer I could get my hands on - no IOException. I set up a new system, just to rule out some special changes to the systems in my company - both buttonx_Click functions worked - no exception either. I even compiled the program on another computer - same behavior. The only three systems to throw that exception were the ones with the changed users folder.</p>
  
  <p>So far I have no clue, why this difference in behavior occurs. Can somebody help me? </p>
  
  <p>Anybody?</p>