Pens and Brushes


This section reviews two helper classes that are needed in order to draw shapes. You’ve already encountered the Pen class, which you used to instruct the graphics instance how to draw lines. A related class is System.Drawing.Brush, which instructs the graphics instance how to fill regions. For example, the Pen is needed to draw the outlines of the rectangle and ellipse in the previous examples. If you had needed to draw these shapes as solid, you would have used a brush to specify how to fill them in. One aspect of both of these classes is that you will hardly ever call any methods on them. You simply construct a Pen or Brush instance with the required color and other properties, and then pass it to drawing methods that require a Pen or Brush.

Tip 

If you’ve programmed using GDI before, you may have noticed from the first couple of examples that pens are used in a different way in GDI+. In GDI the normal practice was to call a Windows API function, SelectObject(), which actually associated a pen with the device context. That pen was then used in all drawing operations that required a pen until you informed the device context otherwise, by calling SelectObject() again. The same principle held for brushes and other objects such as fonts or bitmaps. With GDI+ Microsoft has opted for a stateless model in which there is no default pen or other helper object. Rather, you simply specify with each method call the appropriate helper object to be used for that particular method.

Brushes

GDI+ has several different kinds of brushes - more than there is space to go into in this chapter, so this section just explains the simpler ones to give you an idea of the principles. Each type of brush is represented by an instance of a class derived from the abstract class System.Drawing.Brush. The simplest brush, System.Drawing.SolidBrush, indicates that a region is to be filled with solid color:

  Brush solidBeigeBrush = new SolidBrush(Color.Beige); Brush solidFunnyOrangyBrownBrush =                                new SolidBrush(Color.FromArgb(255,155,100)); 

Alternatively, if the brush is one of the Web-safe colors you can construct the brush using another class, System.Drawing.Brushes. Brushes is one of those classes that you never actually instantiate (it has a private constructor to stop you from doing that). It simply has a large number of static properties, each of which returns a brush of a specified color. You can use Brushes like this:

  Brush solidAzureBrush = Brushes.Azure; Brush solidChocolateBrush = Brushes.Chocolate; 

The next level of complexity is a hatch brush, which fills a region by drawing a pattern. This type of brush is considered more advanced, so it’s in the Drawing2D namespace, represented by the class System.Drawing.Drawing2D.HatchBrush. The Brushes class can’t help you with hatch brushes - you’ll need to construct one explicitly by supplying the hatch style and two colors, the foreground color followed by the background color (you can omit the background color, in which case it defaults to black). The hatch style comes from an enumeration, System.Drawing.Drawing2D.HatchStyle. You can choose from a large number of HatchStyle values (see the SDK documentation for the full list). To give you an idea, typical styles include ForwardDiagonal, Cross, DiagonalCross, SmallConfetti, and ZigZag. Examples of constructing a hatch brush include:

  Brush crossBrush = new HatchBrush(HatchStyle.Cross, Color.Azure); // background color of CrossBrush is black Brush brickBrush = new HatchBrush(HatchStyle.DiagonalBrick,                                   Color.DarkGoldenrod, Color.Cyan); 

Solid and hatch brushes are the only brushes available under GDI. GDI+ has added a couple of new styles of brushes:

  • System.Drawing.Drawing2D.LinearGradientBrush fills in an area with a color that varies across the screen.

  • System.Drawing.Drawing2D.PathGradientBrush is similar, but in this case the color varies along a path around the region to be filled.

Note that both brushes can render some spectacular effects if used carefully.

Pens

Unlike brushes, pens are represented by just one class: System.Drawing.Pen. However, the pen is slightly more complex than the brush, because it needs to indicate how thick lines should be (how many pixels wide) and, for a wide line, how to fill the area inside the line. Pens can also specify a number of other properties, which are beyond the scope of this chapter, but which include the Alignment property mentioned earlier. This property indicates where in relation to the border of a shape a line should be drawn, as well as what shape to draw at the end of a line (whether to round off the shape).

The area inside a thick line can be filled with solid color, or it can be filled using a brush. Hence, a Pen instance might contain a reference to a Brush instance. This is quite powerful, because it means that you can draw lines that are colored in by using, say, hatching or linear shading. You have four different ways to construct a Pen instance that you have designed yourself. You can do it by passing a color, or you can do it by passing in a brush. Both of these constructors will produce a pen with a width of one pixel. Alternatively, you can pass in a color or a brush, and additionally a float, which represents the width of the pen. (It needs to be a float in case you are using nondefault units such as millimeters or inches for the Graphics object that will do the drawing, so you can, for example, specify fractions of an inch.) For example, you can construct pens like this:

 Brush brickBrush = new HatchBrush(HatchStyle.DiagonalBrick,                         Color.DarkGoldenrod, Color.Cyan); Pen solidBluePen = new Pen(Color.FromArgb(0,0,255)); Pen solidWideBluePen = new Pen(Color.Blue, 4); Pen brickPen = new Pen(brickBrush); Pen brickWidePen = new Pen(brickBrush, 10); 

Additionally, for the quick construction of pens, you can use the class System.Drawing.Pens, which, like the Brushes class, contains a number of stock pens. These pens all have a 1-pixel width and come in the usual sets of Web-safe colors. This allows you to construct pens in this way:

  Pen solidYellowPen = Pens.Yellow; 




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net