This class is at the heart of the drawing architecture. It encapsulates a surface on which drawing is performed.

Typically, you are passed a Graphics object in the Paint handler of a Control.

Additionally, you can use the static FromImage() method to get a Graphics surface for one of the classes derived from Image (such as Bitmap or Metafile). Any painting on that surface will be reflected in the image itself.

For interop scenarios, there are static FromHdc() and FromHwnd() methods to construct a Graphics object for a DC or Win32 window, and the corresponding GetHdc() member to retrieve a Win32 device context for the managed surface.

There are a large number of methods that allow you to paint onto the surface. They fall into four broad categories:

  • Outline drawing

  • Surface filling

  • Image compositing

  • Drawing text strings

The outline drawing methods all begin with Draw. The first parameter is a Pen object, which determines the weight, color, and style of the line itself. The remaining parameters vary with the kind of line to be drawn and define the geometric shape in question.

The surface filling methods all begin with Fill. The first parameter is a Brush object, which specifies the color and pattern that will be used to fill the surface. The remaining parameters again vary with the kind of line to be drawn and define the geometric surface to fill.

Most draw and fill methods offer both integer and floating-point versions of the parameters that allow you to define the shapes ...

Get .NET Windows Forms in a Nutshell now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.