title: Right way to dispose Image/Bitmap and PictureBox
body: <p>I am trying to develop a Windows Mobile 6 (in WF/C#) application. There is only one form and on the form there is only a PictureBox object. On it I draw all desired controls or whatever I want. </p>
  
  <p>There are two things I am doing. Drawing custom shapes and loading bitmaps from .png files.</p>
  
  <p>The next line locks the file when loading (which is an undesired scenario):</p>
  
  <pre><code>Bitmap bmp = new Bitmap("file.png");
  </code></pre>
  
  <p>So I am using another way to load bitmap.</p>
  
  <pre><code>public static Bitmap LoadBitmap(string path) {
      using (Bitmap original = new Bitmap(path))
      {
          return new Bitmap(original);
      }
  }
  </code></pre>
  
  <p>This is I guess much slower, but I don't know any better way to load an image, while quickly releasing the file lock.</p>
  
  <p>Now, when drawing an image there is method that I use:</p>
  
  <pre><code>public void Draw() {
      Bitmap bmp = new Bitmap(240,320);
      Graphics g = Graphics.FromImage(bmp);
  
      // draw something with Graphics here.
      g.Clear(Color.Black);
      g.DrawImage(Images.CloseIcon, 16, 48);
      g.DrawImage(Images.RefreshIcon, 46, 48);
      g.FillRectangle(new SolidBrush(Color.Black), 0, 100, 240, 103);
  
      pictureBox.Image = bmp; 
  }
  </code></pre>
  
  <p>This however seems to be some kind of a memory leak. And if I keep doing it for too long, the application eventually crashes.</p>
  
  <p>Therefore, I have 3 questions:</p>
  
  <p><strong>1.)</strong> What is the better way for loading bitmaps from files without locking the file?</p>
  
  <p><strong>2.)</strong> What objects needs to be manually disposed in the Draw() function (and in which order) so there's no memory leak and no ObjectDisposedException throwing?</p>
  
  <p><strong>3.)</strong> If pictureBox.Image is set to bmp, like in the last line of the code, would pictureBox.Image.Dispose() dispose only resources related to maintaining the pictureBox.Image or the underlying Bitmap set to it?</p>