Home

Texture and Gradient Brushes

 

Texture Brushes

 

Introduction

The hatch brushes we have used are based on already designed patterns to fill a shape. In some cases, you may want to create or design your own pattern and use it to fill an area. To do this, you must perform two main steps. First, you must design a picture and save it as a file. Then you must create an object referred to as a texture brush and pass the picture to it.

A texture brush is an object that holds a picture and uses it to regularly fill the interior of a closed shape. To initialize it, you can use an existing picture designed by someone else or you can design your own picture using any normal graphics application, including Microsoft Paint that is automatically installed with Microsoft Window. You should have the picture as a file with a normal graphics file extension, which could be .bmp. Here is an example of a designed bitmap saved as Papers.bmp:

   
Bitmap

Equipped with the picture, you can then use the TextureBrush class that provides various constructors.

Creating a Texture Brush

The simplest constructor of this class takes as argument an Image object. The syntax of this constructor is:

Public Sub New(bitmap As Image)

This constructor expects a bitmap as argument. After initializing the brush, you can use it to fill the interior of a closed shape. For example, you can call a Fill... method to paint its shape. Here is an example:

Imports System.Drawing
Imports System.Windows.Forms

Module Exercise

    Public Class Starter
        Inherits Form

        Dim components As System.ComponentModel.Container

        Public Sub New()
            InitializeComponent()
        End Sub

        Public Sub InitializeComponent()

        End Sub

        Private Sub FormPaint(ByVal sender As Object, _
                              ByVal e As PaintEventArgs) _
                              Handles MyBase.Paint

            Dim BmpPapers As Bitmap = New Bitmap("Papers.bmp")
            Dim BrushPapers As TextureBrush = New TextureBrush(BmpPapers)
            e.Graphics.FillRectangle(BrushPapers, 5, 5, 430, 280)


        End Sub
    End Class

    Function Main() As Integer

        Dim frmStart As Starter = New Starter

        Application.Run(frmStart)

        Return 0
    End Function

End Module

Texture Brush

If you use this constructor, the compiler would itself find out the location and dimensions of the picture in the area where it was designed. Although the location must be at (0, 0), the width and the height must be lower or equal to the intended dimensions of the bitmap. For example, if you have a picture that is 48x48 pixels, the width you can use from this picture must be <= 48 and the height must be <= 48. This allows you to use only a portion of the picture if you want. To use a portion of the picture, you can use the following constructor:

Public Sub New(image As Image, dstRect As Rectangle)

The second argument to this constructor is used to specify the width and height of the picture. If you prefer to use a rectangle whose arguments are decimal numbers, you can use the following constructor:

Public Sub New(image As Image, dstRect As RectangleF)

Here is an example:

Private Sub FormPaint(ByVal sender As Object, _
                              ByVal e As PaintEventArgs) _
                              Handles MyBase.Paint

    Dim BmpPapers As Bitmap = New Bitmap("Papers.bmp")
    Dim BrushPapers As TextureBrush = New TextureBrush(BmpPapers, _
                                      New Rectangle(0, 0, 42, 42))
            e.Graphics.FillRectangle(brushPapers, 5, 5, 416, 290)

End Sub

Texture Brush

Wrapping a Texture Brush

The constructors we have used so far allow you to draw the bitmap in each allocated rectangle in a tiled fashion. To make it more fun, the TextureBrush class is equipped with a factor referred to as wrap mode. This factor specifies how the tiling must be performed. To apply it, you can use one of the following constructors:

Public Sub New(image As Image, wrapMode As WrapMode)
Public Sub New(image As Image, wrapMode As WrapMode, dstRect As Rectangle)
Public Sub New(image As Image, wrapMode As WrapMode, dstRect As RectangleF)

The bitmap and the destination arguments are used in the same way we have done so far. The wrapMode argument is a member of the WrapMode enumerator. The WrapMode enumerator is defined in the System.Drawing.Drawing2D namespace. It has the following members:

Clamp: Draws one picture in the allocated rectangle:

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

Module Exercise

    Public Class Starter
        Inherits Form

        Dim components As System.ComponentModel.Container

        Public Sub New()
            InitializeComponent()
        End Sub

        Public Sub InitializeComponent()

        End Sub

        Private Sub FormPaint(ByVal sender As Object, _
                              ByVal e As PaintEventArgs) _
                              Handles MyBase.Paint

            Dim bmpPapers As Bitmap = New Bitmap("Papers.bmp")
            Dim brushPapers As TextureBrush = New TextureBrush(bmpPapers, _
         			WrapMode.Clamp)
            e.Graphics.FillRectangle(brushPapers, 5, 5, 438, 290)

        End Sub
    End Class

    Function Main() As Integer

        Dim frmStart As Starter = New Starter

        Application.Run(frmStart)

        Return 0
    End Function

End Module

Texture Brush

Tile: Draws the picture continuous in a tiled fashion:

Private Sub FormPaint(ByVal sender As Object, _
                              ByVal e As PaintEventArgs) _
                              Handles MyBase.Paint

            Dim BmpPapers As Bitmap = New Bitmap("Papers.bmp")
            Dim BrushPapers As TextureBrush = New TextureBrush(BmpPapers, _
                                                           WrapMode.Tile, _
                               New Rectangle(0, 0, 42, 42))
            e.Graphics.FillRectangle(BrushPapers, 5, 5, 414, 290)

End Sub

Texture Brush

TileFlipX: Draws the picture, draws it again on its right side but flipped horizontally, and then repeats this technique continuous in a tiled fashion and in the allocated rectangle:

Private Sub FormPaint(ByVal sender As Object, _
                              ByVal e As PaintEventArgs) _
                              Handles MyBase.Paint

            Dim bmpPapers As Bitmap = New Bitmap("Papers.bmp")
            Dim brushPapers As TextureBrush = New TextureBrush(bmpPapers, _
                                                            WrapMode.TileFlipX)
            e.Graphics.FillRectangle(brushPapers, 5, 5, 430, 280)

End Sub

Tile Flip X

TileFlipY: Draws the picture, draws it again under it but flipped vertically, and then repeats this technique continuous in a tiled fashion and in the allocated rectangle:

Private Sub FormPaint(ByVal sender As Object, _
                              ByVal e As PaintEventArgs) _
                              Handles MyBase.Paint

            Dim bmpPapers As Bitmap = New Bitmap("Papers.bmp")
            Dim brushPapers As TextureBrush = New TextureBrush(bmpPapers, _
                                                           WrapMode.TileFlipX, _
                                                	New Rectangle(0, 0, 42, 42))
            e.Graphics.FillRectangle(brushPapers, 5, 5, 412, 290)

End Sub

Tile Flip X

TileFlipXY: Draws the picture, draws it again on its right side but flipped horizontally, then draws both the original and the right copy under each other but flipped vertically. These four pictures are redrawn in a tiled fashion and in the allocated rectangle:

Private Sub FormPaint(ByVal sender As Object, _
                              ByVal e As PaintEventArgs) _
                              Handles MyBase.Paint

            Dim bmpPapers As Bitmap = New Bitmap("Papers.bmp")
            Dim brushPapers As TextureBrush = New TextureBrush(bmpPapers, _
                                                       WrapMode.TileFlipXY, _
                                                    New Rectangle(0, 0, 42, 42))
            e.Graphics.FillRectangle(brushPapers, 5, 5, 412, 244)

End Sub

Texture Brush

Practical LearningPractical Learning: Using Texture Brushes

  1. Start a new Windows Forms Application named SchoolEnrolment2
  2. Design the form as follows:
     
    School Enrolment
    Control Name Text
    Label Label   Enrolment / Program ___________________________
    Label Label   Graduates
    Label Label   Undergraduates
    Label Label   Certificates
    TextBox TextBox txtGraduates 0
    TextBox TextBox txtUndergraduates 0
    TextBox TextBox txtCertificates 0
    Button Button btnCreateChart Create Chart
    PictureBox PictureBox pbxChart  
    Label Label   ____Legend____
    Label Label lblGraduates Graduates
    Label Label lblUndergraduates Undergraduates
    Label Label lblCertificates Certificates
    Button Button btnClose Close
  3. To design a bitmap, on the main menu, click Project -> Add New Item...
  4. In the Templates list, click Bitmap File
  5. Change the Name to graduates and click Add
  6. To change the file location of the bitmap, on the main menu, click File -> Save graduates.bmp As...
  7. Locate and display the SchoolEnrolment2\SchoolEnrolment2\bin\debug folder
  8. Click Save
  9. In the Solution Explorer, double-click graduates.bmp to make sure it is displayed
  10. In the Properties Window, click Width and type 16
  11. Click Height and type 16
  12. Design the bitmap as follows:
     
    Graduates
  13. In the Solution Explorer, right-click the Debug folder -> Add -> New Item...
  14. In the Templates list, make sure Bitmap File is selected.
    Set the Name to undergraduates and click Add
  15. In the Properties window, click Width and type 16
  16. Click Height and type 16
  17. Design the bitmap as follows:
     
    Undergraduates
  18. In the Solution Explorer, right-click the Debug folder -> Add -> New Item...
  19. In the Templates list, make sure Bitmap File is selected.
    Set the Name to certificates and click Add
  20. In the Properties window, click Width and type 16
  21. Click Height and type 16
  22. Design the bitmap as follows:
     
    Certificates
  23. Display the form
  24. Right-click the form and click View Code
  25. Declare three variables as follows:
     
    Public Class Form1
        Dim Graduates As Single
        Dim Undergraduates As Single
        Dim Certificates As Single
    End Class
  26. In the Class Name combo box, select (Form1 Events)
  27. In the Method Name combo box, select Paint and implement the event as follows:
     
    Private Sub Form1_Paint(ByVal sender As Object, _
                            ByVal e As System.Windows.Forms.PaintEventArgs) _
                            Handles Me.Paint
    
        Dim bmpGraduates As Bitmap = New Bitmap("graduates.bmp")
        Dim BrushGraduates As TextureBrush = New TextureBrush(bmpGraduates)
        Dim bmpUndergraduates As Bitmap = New Bitmap("undergraduates.bmp")
        Dim brushUndergraduates As TextureBrush = _
    	New TextureBrush(bmpUndergraduates)
        Dim bmpCertificates As Bitmap = New Bitmap("certificates.bmp")
        Dim brushCertificates As TextureBrush = _
    	New TextureBrush(bmpCertificates)
    
            pbxChart.CreateGraphics().FillPie(BrushGraduates, _
                                      0.0F, 0.0F, _
                              260.0F, 200.0F, 0.0F, Graduates)
    
            pbxChart.CreateGraphics().FillPie(brushUndergraduates, _
                                      0.0F, 0.0F, 260.0F, _
                        200.0F, Graduates, Undergraduates)
    
            pbxChart.CreateGraphics().FillPie(brushCertificates, _
                                      0.0F, 0.0F, 260.0F, _
                          200.0F, Graduates + Undergraduates, _
                              Certificates)
    
            e.Graphics.FillRectangle(BrushGraduates, _
                                  New Rectangle(lblGraduates.Left, _
                                lblGraduates.Top + 18, _
                        btnClose.Width, 20))
    
            e.Graphics.DrawRectangle(New Pen(Color.Black), _
                                  New Rectangle(lblGraduates.Left - 1, _
                                lblGraduates.Top + 18, _
                        btnClose.Width, 20))
    
            e.Graphics.FillRectangle(brushUndergraduates, _
                                  New Rectangle(btnClose.Left, _
                                lblUndergraduates.Top + 18, _
                        btnClose.Width, 20))
    
            e.Graphics.DrawRectangle(New Pen(Color.Black), _
                                  New Rectangle(btnClose.Left - 1, _
                                lblUndergraduates.Top + 18, _
                        btnClose.Width + 1, 20))
    
            e.Graphics.FillRectangle(brushCertificates, _
                                  New Rectangle(btnClose.Left, _
                                lblCertificates.Top + 18, _
                        btnClose.Width, 20))
    
            e.Graphics.DrawRectangle(New Pen(Color.Black), _
                                  New Rectangle(btnClose.Left - 1, _
                                lblCertificates.Top + 18, _
                        btnClose.Width + 1, 20))
    
    
            pbxChart.CreateGraphics().DrawPie(New Pen(Color.Blue), _
                                      0.0F, 0.0F, 260.0F, _
                              200.0F, 0.0F, Graduates)
            pbxChart.CreateGraphics().DrawPie(New Pen(Color.Red), _
                                      0.0F, 0.0F, 260.0F, _
                        200.0F, Graduates, Undergraduates)
            pbxChart.CreateGraphics().DrawPie(New Pen(Color.Green), _
                                      0.0F, 0.0F, 260.0F, _
                          200.0F, Graduates + Undergraduates, _
                              Certificates)
    
            pbxChart.CreateGraphics().DrawEllipse(New Pen(Color.Red, 2), _
                                      New Rectangle(0.0F, 0.0F, 260.0F, 200))
    End Sub
  28. In the Class Name combo box, select pbxChart
  29. In the Method Name combo box, select Paint and implement the event as follows:
     
    Private Sub pbxChart_Paint(ByVal sender As Object, _
                           ByVal e As System.Windows.Forms.PaintEventArgs) _
                               Handles pbxChart.Paint
            Invalidate()
        End Sub
  30. In the Class Name combo box, select btnCreateChart
  31. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnCreateChart_Click(ByVal sender As Object, _
                                         ByVal e As System.EventArgs) _
                                         Handles btnCreateChart.Click
            Dim grad As Single
            Dim under As Single
            Dim cert As Single
            Dim total As Single
            Dim percentGraduates As Single
            Dim percentUndergraduates As Single
            Dim percentCertificates As Single
    
            Try
                grad = CSng(txtGraduates.Text)
            Catch
                MsgBox("Invalid Value")
            End Try
    
            Try
                under = CSng(txtUndergraduates.Text)
            Catch
                MsgBox("Invalid Value")
            End Try
    
            Try
                cert = CSng(txtCertificates.Text)
            Catch
                MsgBox("Invalid Value")
            End Try
    
            total = grad + under + cert
            percentGraduates = (grad / total) * 100
            percentUndergraduates = (under / total) * 100
            percentCertificates = (cert / total) * 100
    
            Graduates = (360 * percentGraduates) / 100
            Undergraduates = (360 * percentUndergraduates) / 100
            Certificates = (360 * percentCertificates) / 100
    
            pbxChart.Invalidate()
    End Sub
  32. Execute the application and test the form
     
     School Enrolment
  33. After using it, close the form

Gradient Brushes

 

Introduction

A gradient brush resembles a solid brush in that it is color-based. Unlike a solid brush, a gradient brush uses two colors. Its main characteristic is that, when used, one color is applied on one side and the other color is applied to the other side. In between, both colors merge to create a transition or fading effect.

There are two types of gradient brushes: linear and path.

Linear Gradient Brushes

A linear gradient is used to apply two colors in a closed shape but from one side of the shape, such as the left, to the other opposite side of the shape, such as the right.

To support linear gradient brushes, the .NET Framework provides the LinearGradientBrush class defined in the System.Drawing.Drawing2D namespace. To specify the starting and the end points inside of the shape that you want to fill, you can use one of the following constructors:

Public Sub New(point1 As Point, _
	         point2 As Point, _
	         color1 As Color, _
	         color2 As Color)
Public Sub New(point1 As PointF, _
	         point2 As PointF, _
	         color1 As Color, _
	         color2 As Color)

The first argument, point1, is the point where the drawing would start. The third argument, color1, is the color that would be applied from that point. The second argument, point2, is the point where the drawing would end by applying the color specified by the fourth argument, color2.

Here is an example:

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

Module Exercise

    Public Class Starter
        Inherits Form

        Dim components As System.ComponentModel.Container

        Public Sub New()
            InitializeComponent()
        End Sub

        Public Sub InitializeComponent()

        End Sub

        Private Sub FormPaint(ByVal sender As Object, _
                              ByVal e As PaintEventArgs) _
                              Handles MyBase.Paint

            Dim lgb As LinearGradientBrush = _
                 New LinearGradientBrush(New Point(20, 20), _
                                         	           New Point(450, 20), _
                                         	           Color.DarkGreen, _
                                         	           Color.LightBlue)
            e.Graphics.FillRectangle(lgb, 20, 20, 430, 180)

        End Sub
    End Class

    Function Main() As Integer

        Dim frmStart As Starter = New Starter

        Application.Run(frmStart)

        Return 0
    End Function

End Module

Linear Brush

By default, the linear gradient brush fills its gradient based on a horizontal line. If you want the color merge to use a different orientation, such as vertical or diagonal, you can use one of the following constructors:

Public Sub New(rect As Rectangle, _
	          color1 As Color, _
	          color2 As Color, _
	          linearGradientMode As LinearGradientMode)
Public Sub New(rect As RectangleF, _
	         color1 As Color, _
	         color2 As Color, _
	         linearGradientMode As LinearGradientMode)

The first argument, rect, is the rectangle inside of which the colors would be applied. The second argument, color1, is the color that would be applied from a starting point. The second argument, color2, is the color that would be applied at the other end. The factor argument is used to determine the orientation of the merging colors. It has the following members:

Vertical: The first color, color1,  is applied to the top section of the rect argument. The second color, color2,  is applied to the bottom section of the rect argument:

Private Sub FormPaint(ByVal sender As Object, _
                              ByVal e As PaintEventArgs) _
                              Handles MyBase.Paint

            Dim rect As Rectangle = New Rectangle(10, 10, 470, 300)
            Dim lgb As LinearGradientBrush = _
                   New LinearGradientBrush(rect, _
                                                    Color.DarkRed, _
                                             Color.White, _
                     LinearGradientMode.Vertical)
            e.Graphics.FillRectangle(lgb, 10, 10, 450, 280)

End Sub

Linear Brush

Horizontal: The first color, color1,  is applied to the left section of the rect argument. The second color, color2,  is applied to the right section of the rect argument

BackwardDiagonal: The first color, color1,  is applied to the top-right corner of the rect argument. The second color, color2,  is applied to the bottom-left corner of the rect argument:

Backward Diagonal

ForwardDiagonal: The first color, color1,  is applied to the top-left corner of the rect argument. The second color, color2, is applied to the bottom-right corner of the rect argument:

Forward Diagonal

The constructor used to produce the above orientation has the limitation that it provides only four options. If you want, you can apply any angular merge as you see fit. To do this, you can use one of the following constructors:

Public Sub New(rect As Rectangle, _
	         color1 As Color, _
	         color2 As Color, _
	         angle As Single)
Public Sub New(rect As RectangleF, _
	         color1 As Color, _
	         color2 As Color, _
	         angle As Single)

The first argument, rect, is the rectangle inside of which the colors would be applied. The last argument, angle, is an angle measured clockwise, that will specify the orientation of the merging colors The second argument, color1, is the color that would be applied from the starting point. The second argument, color2, is the color that would be applied at the other end. Here is an example:

Private Sub FormPaint(ByVal sender As Object, _
                              ByVal e As PaintEventArgs) _
                              Handles MyBase.Paint

            Dim rect As Rectangle = New Rectangle(10, 10, 470, 300)
            Dim lgb As LinearGradientBrush = New LinearGradientBrush(rect, _
                                                         Color.DarkRed, _
                                                         Color.White, -65.24F)
            e.Graphics.FillRectangle(lgb, 10, 10, 450, 280)

End Sub

Linear Brush

Practical LearningPractical Learning: Using a Linear Gradient Brush

  1. Reopen the YearlySales2 project and change the Click event of the Generate button as follows:
     
    Private Sub btnGenerate_Click(ByVal sender As Object, _
                                      ByVal e As System.EventArgs) _
                                      Handles btnGenerate.Click
            ' Retrieve the values of the current year's sales
            Dim CurQtr1 As Integer
            Dim CurQtr2 As Integer
            Dim CurQtr3 As Integer
            Dim CurQtr4 As Integer
    
            Try
                CurQtr1 = CInt(txtCurrentQtr1.Text) / 100
            Catch
                MsgBox("Invalid Value")
            End Try
    
            Try
                CurQtr2 = CInt(txtCurrentQtr2.Text) / 100
            Catch
                MsgBox("Invalid Value")
            End Try
    
            Try
                CurQtr3 = CInt(txtCurrentQtr3.Text) / 100
            Catch
                MsgBox("Invalid Value")
            End Try
    
            Try
                CurQtr4 = CInt(txtCurrentQtr4.Text) / 100
            Catch
                MsgBox("Invalid Value")
            End Try
    
            ' Create an array of Rectangle objects for the current year
            Dim RectCurrentYear As Rectangle() = { _
                    New Rectangle(txtCurrentQtr1.Left + 20, _
                                380 - CurQtr1, 40, CurQtr1), _
                    New Rectangle(txtCurrentQtr2.Left + 20, _
                                 380 - CurQtr2, 40, CurQtr2), _
                    New Rectangle(txtCurrentQtr3.Left + 20, _
                                380 - CurQtr3, 40, CurQtr3), _
                    New Rectangle(txtCurrentQtr4.Left + 20, _
                                380 - CurQtr4, 40, CurQtr4)}
    
            ' Retrieve the values of last year's sales
            Dim PrvQtr1 As Integer
            Dim PrvQtr2 As Integer
            Dim PrvQtr3 As Integer
            Dim PrvQtr4 As Integer
    
            Try
                PrvQtr1 = CInt(txtPreviousQtr1.Text) / 100
            Catch
                MsgBox("Invalid Value")
            End Try
    
            Try
                PrvQtr2 = CInt(txtPreviousQtr2.Text) / 100
            Catch
                MsgBox("Invalid Value")
            End Try
    
            Try
                PrvQtr3 = CInt(txtPreviousQtr3.Text) / 100
            Catch
                MsgBox("Invalid Value")
            End Try
    
            Try
                PrvQtr4 = CInt(txtPreviousQtr4.Text) / 100
            Catch
                MsgBox("Invalid Value")
            End Try
    
            ' Create an array of Rectangle objects for the previous year
            Dim RectPreviousYear As Rectangle() = { _
                    New Rectangle(txtPreviousQtr1.Left + 30, _
                         380 - PrvQtr1, 40, PrvQtr1), _
                    New Rectangle(txtPreviousQtr2.Left + 30, _
                         380 - PrvQtr2, 40, PrvQtr2), _
                    New Rectangle(txtPreviousQtr3.Left + 30, _
                         380 - PrvQtr3, 40, PrvQtr3), _
                    New Rectangle(txtPreviousQtr4.Left + 30, _
                         380 - PrvQtr4, 40, PrvQtr4)}
    
            ' In case the user has changed the values, erase the previous chart
            GraphDrawingArea.Clear(BackColor)
    
            Dim rect As Rectangle = New Rectangle(10, 190, 300, 210)
            Dim linGradBrush As LinearGradientBrush = _
          	New LinearGradientBrush(rect, _
            Color.FromArgb(204, 102, 0), _
            Color.AntiqueWhite, _
                    LinearGradientMode.Vertical)
            GraphDrawingArea.FillRectangle(linGradBrush, rect)
            GraphDrawingArea.DrawRectangle(New Pen(Color.Black), rect)
    
            Dim brushDiagCross As HatchBrush = _
                 New HatchBrush(HatchStyle.DiagonalCross, _
                          Color.White, Color.Blue)
            Dim brushDotDiamond As HatchBrush = _
             New HatchBrush(HatchStyle.DottedDiamond, _
                      Color.Fuchsia, Color.Brown)
    
            ' Draw the chart for the previous year first to send it back
            GraphDrawingArea.FillRectangles(brushDiagCross, _
                                 RectPreviousYear)
            GraphDrawingArea.DrawRectangles(New Pen(Color.Blue), _
                                 RectPreviousYear)
            ' Draw the chart for the current year in front
            GraphDrawingArea.FillRectangles(brushDotDiamond, _
                                 RectCurrentYear)
            GraphDrawingArea.DrawRectangles(New Pen(Color.Red), _
                                 RectCurrentYear)
    
            ' Draw the small rectangles of the legend
            GraphDrawingArea.FillRectangle(brushDotDiamond, _
                            lblCurYear.Left - 30, _
                            lblCurYear.Top, 20, 14)
            GraphDrawingArea.DrawRectangle(New Pen(Color.Red), _
                            lblCurYear.Left - 30, _
                            lblCurYear.Top, 20, 14)
            GraphDrawingArea.FillRectangle(brushDiagCross, _
                            lblLastYear.Left - 30, _
                            lblLastYear.Top, 20, 14)
            GraphDrawingArea.DrawRectangle(New Pen(Color.Blue), _
                            lblLastYear.Left - 30, _
                            lblLastYear.Top, 20, 14)
    
            GraphDrawingArea.DrawRectangle(New Pen(Color.Black), _
                                25, 380, Width - 220, 1)
            Invalidate()
    End Sub
  2. Execute the application to test it:
     
  3. After using the form, close it

Path Gradient Brushes

The second type of gradient brush available is referred to as path gradient. This brush is applied on a path created by connecting a series of points to get a closed shape.  The interior of the shape can then be filled as a gradient.

To support path brushes, the .NET Framework provides the PathGradientBrush from the System.Drawing.Drawing2D namespace. Two of the constructors of this class are:

Public Sub New(points As Point())
Public Sub New(points As PointF())

The argument passed to this constructor is an array of type Point. Here is an example:

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

Module Exercise

    Public Class Starter
        Inherits Form

        Dim components As System.ComponentModel.Container

        Public Sub New()
            InitializeComponent()
        End Sub

        Public Sub InitializeComponent()

        End Sub

        Private Sub FormPaint(ByVal sender As Object, _
                              ByVal e As PaintEventArgs) _
                              Handles MyBase.Paint

            Dim ptGraph As Point() = { _
                    New Point(10, 10), _
                    New Point(450, 10), _
                    New Point(450, 250), _
                    New Point(10, 250)}

            Dim pgb As PathGradientBrush = New PathGradientBrush(ptGraph)
            e.Graphics.FillRectangle(pgb, 10, 10, 450, 280)

        End Sub
    End Class

    Function Main() As Integer

        Dim frmStart As Starter = New Starter

        Application.Run(frmStart)

        Return 0
    End Function

End Module

Path Gradient Brush

 
 
 

Previous Copyright 2008-2009, yevol.com Next