title: Visual C#, process grows until I get memory errors relating to a UserControl using a loop to draw graphics
body: <p>Using Visual C# 2008 Exp Edition, I've noticed that with my project loaded the process is consuming ~70,000K of memory. After a few hours this builds up to about ~500,000K.</p>
  
  <p>At this point a <code>UserControl</code> containing a <code>PictureBox</code> (within a <code>Panel</code>) shows a memory error in Visual C# Express. The picture box contains a bitmap and grid of rectangles, drawn with <code>System.Drawing.Graphics</code>.</p>
  
  <p>Here's the code:</p>
  
  <p>This segement occurs just once when the <code>UserControl</code> is initialised.</p>
  
  <pre><code>Bitmap myBitmap = new Bitmap(a, b);
  Graphics g = null;
  g = Graphics.FromImage(myBitmap);
  g.FillRectangle(Brushes.SteelBlue, 0, 0, c, d);
  
  //Paint Rows &amp; Columns
  for (int x = 0; x &lt;= e - 1; x++)
  {
      for (int y = 0; y &lt;= f - 1; y++)
      {
          g.DrawRectangle(Pens.LightBlue, g, h, i);
      }
  }
  //Release Resources
  g.Dispose();
  //Add bitmap with grid to BG
  ScorePictureBox.Image = myBitmap;
  </code></pre>
  
  <p>This piece of code is quite frequent:</p>
  
  <pre><code>for (int EventIndex = 0; EventIndex &lt;= MidiNoteDownArray.Length - 1; EventIndex++)
  {
      //Paint notes to grid
  
      e.Graphics.FillRectangle(Brushes.LightBlue, j, k, l, m);
      e.Graphics.DrawRectangle(Pens.Purple, o, p, q, r);
  }
  e.Dispose();
  </code></pre>
  
  <p>Am I not releasing resources properly? How can I do this correctly?rrect</p>