
Drawing with GDI+
Previous lessons have touched upon drawing graphics in a couple of places. For example,
Lesson 23 uses a
Turtle class that provides drawing methods, and Lesson 31 explains
printing, which requires that you draw text, lines, and whatever other shapes you want
to print.
However, I’ve mostly avoided discussing graphics code. While I find graphics code a lot of
fun, I have to admit that it’s usually not a high priority for most business applications (aside
from printing).
This lesson explains the basics of graphics programming in C#. It explains the objects that
you use to produce graphics and shows how to use those objects to draw on bitmaps or con-
trols at run time.
Before you can start drawing bar charts or building a new painting application, however, you
need to know about three classes:
Graphics, Pen, and Brush.
GRAPHICS
The Graphics class represents the drawing surface, whether it is a bitmap stored in memory,
a control’s surface on the screen, or a printed page (you saw printing in Lesson 31). In many
ways you can think of
Graphics as the “paper” on which you’re drawing.
The
Graphics class provides the methods that do the actual drawing. Many of them come in
two flavors: one for drawing and one for filling. For example,
DrawRectangle draws a hollow
rectangle and
FillRectangle fills in a rectangle’s interior.
Table 39-1 summarizes some of the most useful of those metho ...