Fill Methods in GDI+ using VB.NET

In this article I will explain about Fill Methods in GDI+.
  • 3707

So far we have seen only the draw methods of the Graphics class. As we discussed earlier, pens are used to draw the outer boundary of graphics, shapes, and brushes are used to fill the interior of graphics shapes. In this section we will cover the Fill methods of the Graphics class. You can fill only certain graphics shapes; hence there are only a few Fill methods available in the Graphics class. Table 3.5 lists them.

The FillCloseCurve Method

FillCloseCurve fills the interior of a closed curve. The first parameter of FillClosedCurve is a brush. It can be solid brush, a hatch brush, or a gradient brush. The second parameter is an array of points. The third and fourth parameters are optional. The third parameter is a fill mode, which is resented by the FillMode enumeration.

The FillMode enumeration specifies the way the interior of a closed path is filled. It has two modes: alternate or winding. The values for alternate and winding are Alternate and Winding, respectively. The default mode is Alternate. The fill mode matters only if the curve intersects itself (see Section 3.2.1.10).

To fill a closed curve using FillClosed Curve, an application first creates a Brush object and an array of points for the curve. The application can then set the fill mode and tension (which is optional) and call FillClosedCurve.

Listing 3.24 creates an array of PointF structures and a SolidBrush object, and calls FillClosedCurve.

LISTING 3.24: Using FillClosedCurve to fill a closed curve


    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
        ' Create a pen
        Dim bluePen As New Pen(Color.Blue, 1)
        ' Create an array of points
        Dim pt1 As New PointF(40.0F, 50.0F)
        Dim pt2 As New PointF(50.0F, 75.0F)
        Dim pt3 As New PointF(100.0F, 115.0F)
        Dim pt4 As New PointF(200.0F, 180.0F)
        Dim pt5 As New PointF(200.0F, 90.0F)
 
        Dim ptsArray As PointF() = {pt1, pt2, pt3, pt4, pt5}

        ' Fill a closed curve
        Dim tension As Single = 1.0F
        Dim flMode As FillMode = FillMode.Alternate
        Dim blueBrush As New SolidBrush(Color.Blue)
        e.Graphics.FillClosedCurve(blueBrush, ptsArray, flMode, tension)
 
        ' Dispose of object
        blueBrush.Dispose()
    End Sub

TABLE 3.5: Graphics fill methods
 

Method Description

FillCloseCurve

Fills the interior of a closed cardinal spline curve defined by an array of Point structures.

FillEllipse

Fills the interior of an ellipse defined by a bounding rectangle specified by a pair of coordinates, a width and a height.

FillPath

Fills the interior of a GraphicsPath object.

FillPie

Fills the interior of a pie section defined by an ellipse specified by a pair of coordinates, a width, a height, and two radial lines.

FillPolygon

Fills the interior of a polygon defined by an array of points specified by Point structures.

FillRectangle

Fills the interior of a rectangle specified by a pair of a coordinates, a width, and a height.

FillRectangles

Fills the interiors of a series of rectangles specified by Rectangle structures.

FillRegion

Fills the interiors of a Region object.


Figure203_36.jpg

FIGURE 3.36: Filing a closed curve

Figure 3.36 shows the output from Listing 3.24.

The FillEllipse Method

FillEllipse fills the interior of an ellipse. It takes a Brush object and rectangle coordinates.

To fill an ellipse using FillEllipse, an application creates a Brush and a rectangle and calls FillEllipse. Listing 3.25 creates three brushes and calls FillEllipse to fill an ellipse with a brush.

LISTING 3.25: Filling ellipses


    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
        Dim g As Graphics = e.Graphics

        ' Create brushes
        Dim redBrush As New SolidBrush(Color.Red)
        Dim blueBrush As New SolidBrush(Color.Blue)
        Dim greenBrush As New SolidBrush(Color.Green)

        'Create a rectangle
        Dim rect As New Rectangle(80, 80, 50, 50)

        ' Fill ellipses
        g.FillEllipse(greenBrush, 40.0F, 40.0F, 130.0F, 130.0F)
        g.FillEllipse(blueBrush, 60, 60, 90, 90)
        g.FillEllipse(greenBrush, 100.0F, 90.0F, 10.0F, 30.0F)

        ' Dispose of objects
        blueBrush.Dispose()
        redBrush.Dispose()
        greenBrush.Dispose()
    End Sub

Figure 3.37 shows the output from Listing 3.25.

Fig3_37.gif

FIGURE 3.37: Filling ellipses

The FillPath Method

FillPath fills the interior of a graphics path. To do this, an application creates Brush and Graphics objects and the calls FillPath, which takes a brush and a graphics path as arguments. Listing 3.26 create GraphicsPath and SolidBrush objects and calls FillPath.

LISTING 3.26: Filling a graphic path


    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)

        ' Create a solid brush
        Dim greenBrush As New SolidBrush(Color.Green)

        ' Create a graphics path
        Dim path As New GraphicsPath()

        ' Add a line to the path
        path.AddLine(20, 20, 103, 80)

        ' Add an ellipse to the path
        path.AddEllipse(100, 50, 100, 100)

        ' Add three more lines
        path.AddLine(195, 80, 300, 80)
        path.AddLine(200, 100, 300, 100)
        path.AddLine(195, 120, 300, 120)

        ' Create a rectangle and call AddRectangle
        Dim rect As New Rectangle(50, 150, 300, 50)
        path.AddRectangle(rect)

        ' Fill path
        e.Graphics.FillPath(greenBrush, path)

        ' Dispose of object
        greenBrush.Dispose()
    End Sub

Figure 3.38 shows the output from Listing 3.26. As the figure shows, the fill method fills all the covered areas of a graphics path.

Figure203_38.jpg

FIGURE: 3.38: Filling a graphics path

The FillPie Method

FillPie fills a pie section with a specified brush. It takes four parameters: a brush, the rectangle of the ellipse, and the start and sweep angles. The following code calls FillPie.


g.FillPie(New SolidBrush(Color.Red), 0F, 0F, 100, 60, 0F, _
90F)

The FillPolygon Method

FillPolygon fills a polygon with the specified brush. It takes three parameters: a brush, an array of points, and a fill mode. The FillMode enumeration defines the fill mode of the interior of the path. It provides two fill modes: Alternate and Winding. The default mode is Alternate.

In our application we will use a hatch brush. So far we have seen only a solid brush. A solid brush is a brush with one color only. A hatch brush is a brush with a hatch style and two colors. These colors work together to support the hatch style. The HatchBrush class represents a hatch brush.

The Code in Listing 3.27 uses FillPolygon to fill a polygon using the Winding mode.

LISTING 3.27: Filling a polygon


        Dim g As Graphics = e.Graphics
 
        ' Create a solid brush
        Dim greenBrush As New SolidBrush(Color.Green)

        ' Create points for polygon
        Dim p1 As New PointF(40.0F, 50.0F)
        Dim p2 As New PointF(60.0F, 70.0F)
        Dim p3 As New PointF(80.0F, 34.0F)
        Dim p4 As New PointF(120.0F, 180.0F)
        Dim p5 As New PointF(200.0F, 150.0F)
        Dim ptsArray As PointF() = {p1, p2, p3, p4, p5}

        ' Draw polygon
        e.Graphics.FillPolygon(greenBrush, ptsArray)

        ' Dispose of objects
        greenBrush.Dispose()


Figure 3.39 shows the output from Listing 3.27. As you can see, the fill method fills all the areas of a polygon.

Filling Rectangle and Regions

FillRectangle fills a rectangle with a brush. This method takes a brush and a rectangle as arguments. FillRectangles fills a specified series of rectangles with a brush, and it takes a brush and an array of rectangles. These methods also have overloaded forms with additional options. For instance, if you're using a HatchStyle brush, you can specify background and foreground colors.

Note: The HatchBrush class is defined in the System.Drawing.Drawing2D namespace.

The source code in Listing 3.28 uses FillRectangle to fill two rectangles. One rectangle is filled with a hatch brush, the other with a solid brush.

LISTING 3.28: Filling rectangle


    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)

        ' Create brushes
        Dim blueBrush As New SolidBrush(Color.Blue)

        ' Create a rectangle
        Dim rect As New Rectangle(10, 20, 100, 50)

        ' Fill rectangle
        e.Graphics.FillRectangle(New HatchBrush(HatchStyle.BackwardDiagonal, Color.Yellow, Color.Black), rect)
        e.Graphics.FillRectangle(blueBrush, New Rectangle(150, 20, 50, 100))

        ' Dispose of object
        blueBrush.Dispose()
    End Sub

FillRegion fills a specified region with a brush. This method takes a brush and a region as input parameters. Listing 3.29 creates a Region object from a rectangle and calls FillRegion to fill the region.

LISTING 3.29: Filling regions


Dim rect As New Rectangle(20, 20, 150, 100)
Dim rgn As New Region(rect)
e.Graphics.FillRegion(New SolidBrush(Color.Green), rgn)


Conclusion

Hope the article would have helped you in understanding Fill Methods in GDI+. Read other articles on GDI+ on the website.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.