Brushes, Part 1.

The GDI+ Brush object is used to fill areas with colour. Ahh if only life were that simple. Like their cousins Pens, Brushes have some pretty complex behaviours that will require more than a simple introduction. In this article you'll see some of the simpler functions of the brush object and explore a few of the possibilities they offer. In a later article you'll see some of the really neat stuff they can do.

Just as Pens have a set of stock objects, so do Brushes. The Brushes collection contains one solid brush for each of the standard system colours. You can access them by using say, Brushes.Red.

The SolidBrush.

The Brush base-class is abstract. That is to say that it cannot be used as-is because it lacks some implementation details although the way the class interfaces work has been defined. Concrete examples of brushes that do work have been derived from the Brush base class and the simplest of these is the SolidBrush object.

As it's name suggests, this object is used to paint areas with solid colour and is quite simple to use. The initialization is easy, all you have to do is pick a colour. The following listing illustrates this.

SolidBrush sb=new SolidBrush(Color.Blue);
Dim sb As New SolidBrush(Color.Blue)

The brush may be used to fill any shape drawn with GDI+. Figure 1 shows a few shapes filled with solid brushes. The following listing shows the code.

Figure 1. The SolidBrush filling shapes.

    protected override void OnPaint(PaintEventArgs e)

    {

 

      SolidBrush sb=new SolidBrush(Color.Blue);

      e.Graphics.FillRectangle(sb,10,10,200,150);

      e.Graphics.FillEllipse(sb,220,10,200,150);

      e.Graphics.FillPolygon(sb,new Point[]{

                           new Point(110,160),

                           new Point(10,310),

                           new Point(220,310)

                         });

      Pen p=new Pen(sb,10);

      e.Graphics.DrawLines(p,new Point[]{

                          new Point(360,160),

                          new Point(230,310),

                          new Point(430,310)});

      p.Dispose();

      sb.Dispose();

      base.OnPaint (e);

    }

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)

      Dim sb As New SolidBrush(Color.Blue)

      e.Graphics.FillRectangle(sb, 10, 10, 200, 150)

      e.Graphics.FillEllipse(sb, 220, 10, 200, 150)

      e.Graphics.FillPolygon(sb, New Point() {New Point(110, 160), _

                                              New Point(10, 310), _

                                              New Point(220, 310)})

      Dim p As New Pen(sb, 10)

      e.Graphics.DrawLines(p, New Point() {New Point(360, 160), _

                                           New Point(230, 310), _

                                           New Point(430, 310)})

      p.Dispose()

      sb.Dispose()

      MyBase.OnPaint(e)

    End Sub 'OnPaint

 

An interesting point to note is that the brush can be used to fill the outline generated by a Pen object. Remember from this article that Pens can create complex shapes? These shapes can be filled with any brush making the Pen-Brush combination very powerful indeed.

Down the hatch.

Another brush type derived from Brush is the HatchBrush. This brush enables you to fill shapes with one of the standard hatch styles provided by the system. Hatch patterns are repeating pictures, 8 by 8 pixels square, that tessellate to cover a plane in a seamless texture. Many of the patterns represent brick or trellis-work and can be used to provide something other than a plain field to look at. The HatchBrush may be constructed with one of the hatch-patterns shown in Figure 2.

Figure 2. Hatch styles.

To use a hatch brush in your code you just need to create the brush and choose the colours with which the foreground and background will be painted. In Figure 2 the backgrounds are white and the foregrounds are black but you can of course chose any colour combination. Figure 3 shows the identical code used to draw the SolidBrush example but this time a HatchBrush will be used to fill the shapes instead.

Figure 3. Same shapes, different brush.

In this instance, the brush uses the DiagonalBrick hatch style and employs Brown and Gray for the background and foreground colours. The following listing shows the code that drew this image.

    protected override void OnPaint(PaintEventArgs e)

    {

      HatchBrush hb=new HatchBrush(HatchStyle.DiagonalBrick, Color.Gray, Color.Brown);

      e.Graphics.FillRectangle(hb,10,10,200,150);

      e.Graphics.FillEllipse(hb,220,10,200,150);

      e.Graphics.FillPolygon(hb,new Point[]{

                           new Point(110,160),

                           new Point(10,310),

                           new Point(220,310)

                         });

      Pen p=new Pen(hb,10);

      e.Graphics.DrawLines(p,new Point[]{

                          new Point(360,160),

                          new Point(230,310),

                          new Point(430,310)});

      p.Dispose();

      hb.Dispose();

      base.OnPaint (e);

  }

 

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)

 

      Dim hb As New HatchBrush(HatchStyle.DiagonalBrick, Color.Gray, Color.Brown)

      e.Graphics.FillRectangle(hb, 10, 10, 200, 150)

      e.Graphics.FillEllipse(hb, 220, 10, 200, 150)

      e.Graphics.FillPolygon(hb, New Point() {New Point(110, 160), _

                                              New Point(10, 310), _

                                              New Point(220, 310)})

      Dim p As New Pen(hb, 10)

      e.Graphics.DrawLines(p, New Point() {New Point(360, 160), _

                                           New Point(230, 310), _

                                           New Point(430, 310)})

      p.Dispose()

      hb.Dispose()

      MyBase.OnPaint(e)

    End Sub 'OnPaint

 

Painting with pictures.

The last type of brush for this article is the TextureBrush. This object is once-again derived from the abstract Brush class and enables you to fill an area with the contents of an image. This is particularly useful if you can obtain a seamlessly tiling texture such as a marble or brickwork image. The texture shown in Figure 4 was generated by Sausage Software's Reptile program and will tile to cover any area.

Figure 4. A marbled tile.

 The TextureBrush requires an image at construction which you can load from disk like this...

Image img=Image.FromFile("imagefile.bmp");

TextureBrush tb=new TextureBrush(img);

Dim img As Image=Image.FromFile("imagefile.bmp")

Dim tb As New TextureBrush(img)

This brush may be used to draw the same set of figures but this time with an image instead of a colour or a hatch pattern. Figure 5 shows the application in action.

Figure 5. Tiling textures.

Exercise.

Modify the code shown in one of the previous listings to display something like the image in Figure 5. If you wish you can save the tiling marble texture in Figure 4 by right clicking on it and saving the image to your local file system.

Summary.

So far you've seen how to use simple brushes to fill areas with colour, a hatch-pattern or an image. These capabilities, while impressive, only scratch the surface of the Brush based family of objects and later articles will deal with more complex issues.

Return to the Beginners Guide.