title: Drawing in C# (Offscreen)
body: <p>I want to program a little game in C#. At the moment there are only squares involved, but I'll come to the details later on.</p>
  
  <p>So there is a lot of stuff going on in the background, like collision detecting, bullet movement, player movement, focusing with the "camera" on the player (and enemy AI in the future). To execute all these tasks I have a timer with an intervall of 10 milliseconds. With each tick, physics "happen" and the screen will be redrawn.</p>
  
  <p>Now to the problem: The screen is flickering,... not that bad but I think it will be worse when I add more things. I've tried to draw to image offscreen and draw it afterwards, as a whole image to the screen. But I need to create several Bitmap and Graphics objects, and after about 30 seconds the program will use like 2GB of RAM.</p>
  
  <pre><code>public void Draw()
      {
          Bitmap bmpMap = new Bitmap(Map.Width, Map.Height);
          Graphics gphMap = Graphics.FromImage(bmpMap);
          Bitmap bmpWeapon = new Bitmap(WeaponSector.Width, WeaponSector.Height);
          Graphics gphWeapon = Graphics.FromImage(bmpWeapon);
  
          DrawMap(gphMap);
          DrawWeaponSector(gphWeapon);
  
          Map.CreateGraphics().DrawImage(bmpMap, 0, 0);
          gphMap.Dispose();
          WeaponSector.CreateGraphics().DrawImage(bmpWeapon, 0, 0);
          gphWeapon.Dispose();
      }
  </code></pre>
  
  <p>Note: I don't draw directly on the form, but in Panels. They are called Map (here is the action going on) and WeaponSector (here you can see your current weapon and ammo).</p>