Fill Methods in GDI+


This article has been excerpted from book "Graphics Programming with GDI+".

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
void Form1_Paint (object sender,
            System.Windows.Forms.PaintEventArgs e)
      {
            // Create a pen
            Pen bluePen = new Pen (Color.Blue, 1);
            // Create an array of points
            PointF pt1 = new PointF(40.0F, 50.0F);
            PointF pt2 = new PointF(50.0F, 75.0F);
            PointF pt3 = new PointF(100.0F, 115.0F);
            PointF pt4 = new PointF(200.0F, 180.0F);
            PointF pt5 = new PointF(200.0F, 90.0F);
           
            PointF[] ptsArray =
            {
            pt1, pt2, pt3, pt4, pt5
            };

            // Fill a closed curve
            float tension = 1.0F;
            FillMode flMode = FillMode.Alternate;
            SolidBrush blueBrush = new SolidBrush(Color.Blue);
            e.Graphics.FillClosedCurve(blueBrush,ptsArray,flMode,tension);

           // Dispose of object
            blueBrush.Dispose();
      }


TABLE 3.5: Graphics fill methods

MethodDescription

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.


Figure 3.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
void Form1_Paint(object sender,
            System.Windows.Forms.PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            // Create brushes
            SolidBrush redBrush = new SolidBrush(Color.Red);
            SolidBrush blueBrush = new SolidBrush(Color.Blue);
            SolidBrush greenBrush = new SolidBrush(Color.Green);

            //Create a rectangle
            Rectangle rect = 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();
        }


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 void Form1_Paint(object sender,
                    System.Windows.Forms.PaintEventArgs e)

        {

            // Create a solid brush
            SolidBrush greenBrush = new SolidBrush(Color.Green);

            // Create a graphics path
            GraphicsPath path = 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
            Rectangle rect = new Rectangle(50, 150, 300, 50);
            path.AddRectangle(rect);

            // Fill path
            e.Graphics.FillPath(greenBrush, path);

            // Dispose of object
            greenBrush.Dispose();
        }

}

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.

Figure 3.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),
0.0F, 0.0F, 100, 60, 0.0F, 90.0F);


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


Graphics g = e.Graphics;


// Create a solid brush

SolidBrush
greenBrush = new SolidBrush (Color.Green);

// Create points for polygon

PointF
p1 = new PointF (40.0F, 50.0F);
PointF
p2 = new PointF (60.0F, 70.0F);
PointF
p3 = new PointF (80.0F, 34.0F);
PointF
p4 = new PointF (120.0F, 180.0F);
PointF
p5 = new PointF (200.0F, 150.0F);
PointF
[] ptsArray =
{
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
void Form1_Paint(object sender,
            System.Windows.Forms.PaintEventArgs e)
        {

            // Create brushes
            SolidBrush blueBrush = new SolidBrush(Color.Blue);

            // Create a rectangle
            Rectangle rect = 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();
        }


Figure 3.40 shows the output from Listing 3.28

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


Rectangle rect = new Rectangle (20, 20, 150, 100);

Region
rgn = 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.

bookGDI.jpg
This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows.