title: WPF with GDI Memory growth
body: <p>I'm writing an app in WPF (C#) that plots certain types of fractals.  The data sets are 3d points and need to represented as single pixels.  The user can click and drag to rotate the plots  The amount of points varies but can be quite large (five million plots or more).  Currently I am drawing onto a System.Drawing.Bitmap using a Graphics object then setting a WPF Image's Source property to that bitmap.  The problem I have is that when i repeatedly redraw the image   memory consumption steadily climbs.  The Plot() method is called repeated when the user is dragging the mouse.</p>
  
  <pre><code>public void Plot()
  {
      Graphics g = Graphics.FromImage(bmpCanvas);
  
      //Draw on the graphics object
      //...
      //...
      //...
  
      g.Dispose();
      Canvas.Source = loadBitmap(bmpCanvas);
      GC.Collect();
  }
  
  private BitmapSource loadBitmap(System.Drawing.Bitmap source)
  {
      return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(source.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty,
          System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
  }
  </code></pre>
  
  <p>Specifically I want to know what I can do to prevent the memory from climbing uncontrollably.  </p>
  
  <p>In general I want to know if this a good approach in the first place.  I want to display custom drawn images inside a wpf app and be able to do so quickly (IE. a decent frame rate)</p>
  
  <p>Is there a more native way to do this inside the WPF framework?  Should I ditch WPF and go back to windows forms where I can control things more precisely?</p>