Pens and Brushes

 
Chapter 19 - Graphics with GDI+
bySimon Robinsonet al.
Wrox Press 2002
  

In this section, we'll review two helper classes that are needed in order to draw shapes . We've already encountered the Pen class, used to tell the graphics instance how to draw lines. A related class is System.Drawing.Brush , which tells it how to fill regions . For example, the Pen is needed to draw the outlines of the rectangle and ellipse in our previous samples. If we'd needed to draw these shapes as solid, it would have been a brush that would have been used 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 .

We will look at brushes first, then pens.

Incidentally, if you've programmed using GDI before you 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+, as mentioned earlier, Microsoft has instead gone 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 brush more than we have space to go into in this chapter, so we'll just explain 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 , simply 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 Internet named colors you can construct the brush more simply using another class, System.Drawing.Brushes . Brushes is one of those classes that you never actually instantiate (it's got a private constructor to stop you doing that). It simply has a large number of static properties, each of which returns a brush of a specified color. You'd 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 . There are a large number of HatchStyle values available, so it's easiest to refer to the MSDN 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 brush:

  • 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

We won't go into these brushes in this chapter. We'll note though that both can give some spectacular effects if used carefully .

Pens

Unlike brushes, pens are represented by just one class System.Drawing.Pen . The pen is, however, actually 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 that we mentioned earlier, which 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 may contain a reference to a Brush instance. This is quite powerful, as it means you can draw lines that are colored in by using say hatching or linear shading. There are four different ways that you can 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 we are using non-default units such as millimeters or inches for the Graphics object that will do the drawing so we can for example specify fractions of an inch.) So 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, simply contains a number of stock pens. These pens all have width one pixel and come in the usual sets of Internet named colors. This allows you to construct pens in this way:

   Pen solidYellowPen = Pens.Yellow;   
  


Professional C#. 2nd Edition
Performance Consulting: A Practical Guide for HR and Learning Professionals
ISBN: 1576754359
EAN: 2147483647
Year: 2002
Pages: 244

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