The .NET Framework offers GDI+, a simple, yet powerful way of drawing graphics. A lot of people have never even heard of it, and just use PictureBoxes instead. But what about when you're making a serious game? Or a photo editor? That's where GDI+ comes in.

The Graphics object
GDI+ is all encapsulated in a class called "Graphics", in the System.Drawing namespace. There are quite a few ways to get a Graphics object. Controls have the CreateGraphics method, but this is not good to use for a few reasons. The most important one is that graphics drawn this way will be erased the next time the control is re-drawn, that is when part of it is shown after being hidden or when Invalidate is called on it. There are also Paint events of controls, but these cause flicker if you're making any kind of game or serious graphics application. The last one is inherited from Control, and is flickerless if you set DoubleBuffered to True on your control. This is the one I will use in the examples.

Anyways, the Graphics object has a set of interesting methods for drawing pretty much anything. Some simple ones are:
  • DrawLine(Pen, Starting X, Starting Y, Ending X, Ending Y) or DrawLine(Pen, Start Point, End Point)
  • DrawRectangle(Pen, X, Y, Width, Height) or DrawRectangle(Pen, Location, Size)


All of the shape methods have FillXXX counterparts as well.
There's also DrawImage, for drawing an Image object on to a Graphics surface.