You're just getting started with GDI+ graphics and want to know where to begin.
Sample code folder: Chapter 09\GDIObjects
Always start by defining and creating the fundamental graphics objects relied upon by all GDI+ graphics methods. These include colors, pens, fonts, brushes, and of course the Graphics object itself, the drawing surface used by all graphics drawing methods.
The sample code in this recipe demonstrates the creation of several graphics-related objects, providing a good starting point for studying some GDI+ fundamentals. We'll look at the code in sections.
The most common place to put drawing code is in the Paint event handler for the form or control on which you will draw:
Private Sub Form1_Paint(ByVal sender As Object, _ ByVal e As System.Windows.Forms.PaintEventArgs) _ Handles Me.Paint
You can draw in other events or methods as well, but you'll run into fewer hassles if you paint when the system tells you to, rather than forcing redrawing of surfaces based on other events.
The Paint event provides a couple of useful parameters to help with the painting. You can create your own Graphics objecta technique handy in some situationsbut when drawing in a Paint event, simply use the Graphics object passed to the event. You can reference the e.Graphics object by that nomenclature, or you can create a shorter reference to it (such as, in this example, canvas):
' ----- Grab the graphics object for this form. Dim canvas As Graphics = e.Graphics
You typically use the Graphics object a lot in the Paint method, so keeping the reference easy to use can simplify your coding.
Colors can be defined in several ways, some of which are demonstrated in the following group of program lines. You can choose from a long list of enumerated colors with fanciful names like "cornsilk," or you can build your own color by setting each of the red, green, and blue components of the color to a value from 0 to 255. There are also some named system colors you can access to employ the standard colors selected by the user for the entire workstation. The advantage of using these colors is that your graphics will take on the system-described colors, even if the user has changed one of those colors from its default base. A fourth optional parameter (actually passed as the first argument to Color.FromArgb())), called Alpha, controls the transparency of a color. As shown in the following code, a transparent shade of green is created by setting its Alpha parameter to a middle-of-the-road value of 127:
' ----- Create some colors. Dim colorBackground As Color = Color.Cornsilk Dim colorRed As Color = Color.FromArgb(255, 0, 0) Dim colorTransparentGreen As Color = _ Color.FromArgb(127, 0, 255, 0) Dim colorControlDark As Color = _ SystemColors.ControlDark
A Pen is used as a parameter for many drawing methods. For example, lines, ellipses, rectangle edges, and polygon edges are all drawn using a designated pen to define the lines used to construct them. A basic Pen object is comprised of a color and an optional width. If not given, the width defaults to 1 unit, and you'll get what you expect if your scaling mode is the default pixels. If a different scaling is used, the thickness of the pen's line will remain at 1 unit, but depending on the scaling this can drastically affect the appearance of the lines you draw (see Recipe 9.8 for more on this topic). The following code block defines pen1 with a width of 1 unit and pen2 with a width of 25:
' ----- Create some pens. Dim pen1 As New Pen(Color.Blue) Dim pen2 As New Pen(colorRed, 25)
Font objects are required whenever text is drawn on a graphics surface. There are several ways to define a new Font object: you can specify its name and a few optional properties such as font size, or you can start with a given font and make changes to it. Both of these techniques are used in the program lines shown here:
' ----- Create some fonts. Dim font1 As New Font("Arial", 24, _ FontStyle.Bold Or FontStyle.Italic) Dim font2 As New Font(Me.Font, FontStyle.Regular)
Visual Basic 2005 doesn't have a plain old Print command, like the one that was available in the good old days of VB 6. You'll need to become familiar with fonts, brushes, and GDI+ methods such as DrawString() to draw even the simplest text content. The upside of this situation is that text can be drawn on any surface in the same way, whether it's a printer page, a form, or the face of a button or other control.
When you draw shapes using lines, you pass the graphics method a pen. When you fill Graphics objects with color, such as when drawing a solid-filled rectangle or ellipse, you pass a brush. Brushes can be solid-filled with a color, as shown here, or they can be created using a repeating fill pattern or image:
' ----- Create some brushes. Dim brush1 As New SolidBrush(Color.DarkBlue) Dim brush2 As New SolidBrush(colorTransparentGreen)
The next lines use several methods of the Graphics object to render ellipses, rectangles, and a string:
' ----- Demonstrate a few sample graphics commands. canvas.Clear(colorBackground) canvas.DrawEllipse(pen2, 100, 50, 300, 200) canvas.FillEllipse(brush1, New Rectangle( _ 50, 150, 250, 200)) canvas.FillRectangle(New SolidBrush(colorTransparentGreen), _ 120, 30, 150, 250) canvas.DrawString("Text is drawn using GDI+", _ font1, brush1, 120, 70)
Figure 9-1 displays the results generated by this code. The biggest ring is a single-line outline of an ellipse, drawn using the pen2 object defined above (it's actually a red pen with a width of 25 unitsin this case, the units are the default pixels). The lower ellipse is solid-filled using a blue brush. Clipping takes place automatically, and although the blue ellipse doesn't quite fit on the form's surface, this causes no problems. The rectangle uses the transparent green brush defined earlier, allowing the red and blue ellipses to show through from underneath. Finally, the string of text can be drawn at any location, using any font, any size, any color, and any rotation angle.
Figure 9-1. Creating GDI+ graphics
Proper GDI+ etiquette requires that you properly dispose of all objects you create. Back in the old days before Windows 95, proper cleanup of graphics objects was essential, and the system could crash if it ran out of its few precious graphics resources. Those fears are long gone, but GDI+ objects still consume system resources. The .NET garbage-collection system will eventually dispose of all graphics objects, but it's best if you do it yourself immediately:
' ----- Clean up. brush2.Dispose() brush1.Dispose() font2.Dispose() font1.Dispose() pen2.Dispose() pen1.Dispose() canvas = Nothing End Sub