You have seen the basics of displaying images on your forms; now let’s move on to some real graphics operations, namely how to create your own graphics with the Framework. Windows graphics are based on a graphics engine, known as GDI. GDI, which stands for Graphics Design Interface, is a collection of classes that enable you to create graphics, text, and images. The most recent version on GDI is called GDI+.
One of the basic characteristics of GDI is that it’s stateless. This means that each graphics operation is totally independent of the previous one and can’t affect the following one. To draw a line, you must specify a Pen object and the two endpoints of the line. You must do the same for the next line you’ll draw. You can’t assume that the second line will use the same pen or that it will start at the point where the previous line ended. There isn’t even a default font for text-drawing methods. Every time you draw some text, you must specify the font in which the text will be rendered, as well as the Brush object that will be used to draw the text.
The GDI+ classes reside in the following namespaces, and you must import one or more of them in your projects: System.Drawing, System.Drawing2D, System.Drawing.Imaging, and System.Drawing.Text. This chapter explores all three aspects of GDI+ — namely vector drawing, imaging, and typography.
Before you start drawing, you must select the surface you want to draw on, the types of shapes you want to draw, and the instrument you’ll use to draw them. The surface on which you can draw is a Graphics object, which is your canvas, and it’s the control’s Graphics property. Most controls expose a Graphics property, but most applications draw on either forms or PictureBox controls. The Graphics property is an object that exposes numerous methods for drawing basic (and not-so-basic) shapes.
The next step is to decide which instrument you’ll use to draw. There are two major drawing instruments: the Pen object and the Brush object. You use pens to draw stroked shapes (lines, rectangles, curves) and brushes to draw filled shapes (any area enclosed by a shape, including text). The main characteristics of the Pen object are its color and its width (the size of the trace left by the pen). The main characteristic of the Brush object is the color or pattern with which it fills the shape. An interesting variation of the Brush object is the gradient brush, which changes color as it moves from one point of the shape you want to fill to another. You can start filling a shape with red in the middle and specify that as you move toward the edges of the shape, the fill color fades to yellow.
After you have specified the drawing surface and the drawing instrument, you draw the actual shape by calling the appropriate method of the Graphics object. To draw lines, call the DrawLine method of the Graphics object; to draw text, call the DrawString method of the same object. There are many drawing methods, as well as other methods that support the main drawing methods, and they’re all discussed later in this chapter. Here are the statements to draw a line on the form:
Dim redPen As Pen = New Pen(Color.Red, 2)
Dim point1 As Point = New Point(10,10)
Dim point2 As Point = New Point(120,180)
Me.CreateGraphics.DrawLine(redPen, point1, point2)
The first statement declares a new Pen object, which is initialized to draw in red with a width of 2 pixels. The following two statements declare and initialize two points, which are the line’s starting and ending points. The coordinates are expressed in pixels, and the origin is at the form’s top-left corner. The last statement draws the line by calling the DrawLine method. The expression Me.CreateGraphics retrieves the Graphics object of the form, which exposes all the drawing methods, including the DrawLine method. You can also create a new Graphics object and associate it with the form:
' set up a pen and the two endpoints as before
Dim G As Graphics
G = Me.CreateGraphics
G.DrawLine(redPen, point1, point2)
The DrawLine method accepts as an argument the pen it will use to draw and the line’s starting and ending points. I have used two Point objects to make the code easier to read. The DrawLine method, like all other drawing methods, is heavily overloaded. You can also omit the declarations of the various objects and initialize them in the same statement that draws the line:
Me.CreateGraphics.DrawLine(New Pen(Color.Red, 2), _
New Point(10, 10), New Point(120, 180))
All coordinates are expressed by default in pixels. It’s possible to specify coordinates in different units and let GDI+ convert them to pixels before drawing. For now, we’ll use pixels, which are quite appropriate for simple objects. After you familiarize yourself with the drawing methods, you can specify different coordinate systems. For more information, see the discussion of the PageUnit property of the Graphics object in the next page.