[Lesson 25] << [CONTENTS] >> [Lesson 27]
In this lesson, we will show you about filling shapes with color, or simply solid shapes.
Three methods that are used to fill shapes are FillRectangle, FillEllipse, FillPolygon and FillPie.
In order to fill the above shapes with color, we need to create the Brush object using the following syntax:
myBrush = New SolidBrush(Color.myColor)
*myColor can be any color such as red, blue, yellow and more.
26.1 Drawing and Filling a Rectangle
The syntax to fill a rectangle with the color defined by the brush object is:
myGraphics.FillRectangle (myBrush, 0, 0, 150, 150)
The complete code is shown in the example below:
Example 26.1
Dim myPen As Pen Dim myBrush As Brush Dim myGraphics As Graphics = Me.CreateGraphics myPen = New Pen(Drawing.Color.Blue, 5) myBrush = New SolidBrush(Color.Coral) myGraphics.DrawRectangle(myPen, 0, 0, 150, 150) myGraphics.FillRectangle(myBrush, 0, 0, 150, 150)
The Output is shown below:
26.2 Drawing and Filling an Ellipse
The syntax to fill a ellipse with the color defined by the brush object is:
myGraphics.FillEllipse (myBrush, 0, 0, 150, 150)
The complete code is shown in the example below:
Example 26.2
Dim myPen As Pen Dim myBrush As Brush Dim myGraphics As Graphics = Me.CreateGraphics myPen = New Pen(Drawing.Color.Blue, 5) myBrush = New SolidBrush(Color.Coral) myGraphics.DrawEllipse(myPen, 0, 0, 150, 180) myGraphics.FillEllipse(myBrush, 0, 0, 150, 180)
The output is shown below:
26.3 Drawing and Filling a Polygon
The syntax to fill a polygon with the color defined by the brush object is:
myGraphics.FillPolygon(myBrush, myPoints)
The complete code is shown in the example below:
Dim myPen As Pen Dim myBrush As Brush Dim A As New Point(10, 10) Dim B As New Point(100, 50) Dim C As New Point(120, 150) Dim D As New Point(60, 200) Dim myPoints As Point() = {A, B, C, D} myPen = New Pen(Drawing.Color.Blue, 5) myBrush = New SolidBrush(Color.Coral) Dim myGraphics As Graphics = Me.CreateGraphics myGraphics.DrawPolygon(myPen, myPoints) myGraphics.FillPolygon(myBrush, myPoints)
Running the code produces the image below:
26.4 Drawing and Filling a Pie
The syntax to fill a pie with the color defined by the brush object is:
myGraphics.FillPie(myBrush, X, Y, width, height, StartAngle, SweepAngle)
The complete code is shown in the example below:
Dim myPen As Pen Dim myBrush As Brush myPen = New Pen(Drawing.Color.Blue, 5) myBrush = New SolidBrush(Color.Coral) Dim myGraphics As Graphics = Me.CreateGraphics myGraphics.DrawPie(myPen, 30, 40, 150, 150, 0, 60) myGraphics.FillPie(myBrush, 30, 40, 150, 150, 0, 60)
The output is shown below: