The PathGradientBrush object is used to fill a graphics path with a gradient. We can specify the center and boundary colors of a path.
The CenterColor and SurroundColors properties are used to specify the center and boundary colors. Listing 9.28 uses the CenterColor and SurroundColors properties; it sets the center color of the path to red and the surrounding color to green.
LISTING 9.28: Blending using PathGradientBrush
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Public Class Form1
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim g As Graphics = Me.CreateGraphics()
g.Clear(Me.BackColor)
' Create Blend object
Dim blend As New Blend()
' Create point and positions arrays
Dim factArray As Single() = {0.0F, 0.3F, 0.5F, 1.0F}
Dim posArray As Single() = {0.0F, 0.2F, 0.6F, 1.0F}
' Set Factors and Positions properties of Blend
blend.Factors = factArray
blend.Positions = posArray
' Set smoothing mode of Graphics object
g.SmoothingMode = SmoothingMode.AntiAlias
' Create path and add a rectangle
Dim path As New GraphicsPath()
Dim rect As New Rectangle(10, 20, 200, 200)
path.AddRectangle(rect)
' Create path gradient brush
Dim rgBrush As New PathGradientBrush(path)
' Set Blend and FocusScales properties
rgBrush.Blend = blend
rgBrush.FocusScales = New PointF(0.6F, 0.2F)
Dim colors As Color() = {Color.Green}
' Set CenterColor and SurroundColors properties
rgBrush.CenterColor = Color.Red
rgBrush.SurroundColors = colors
g.FillEllipse(rgBrush, rect)
' Dispose of object
g.Dispose()
End Sub
End Class
If you run the code from Listing 9.28, you will see that the focus is the center of the ellipse and there is scattering in a faded color toward the boundary of the ellipse. The center is red, and the border is green (see Figure 9.37).
The FocusScales property changes the focus point for the gradient falloff. The following code snipped sets the FocusScales property.
rgBrush.FocusScales = New PointF(0.6F, 0.2F)
After FocusScales is set the color of the ellipse changes from the center of the ellipse to a rectangle. Figure 9.38 shows the new output.
FIGURE 9.37: Blending using PathGradientBrush
We can even specify multiple surrounding colors. For example, we can create an array of different colors and use them for the SurroundedColors property of the brush. To do so, we replace the following line of Listing 9.28.
Dim colors As Color() = {Color.Green}
with the following code snipped:
Dim colors As Color() = {Color.Green, Color.Blue, Color.Red, Color.Yellow}
rgBrush.SurroundColors = colors
If you add this code to the application, you will see a totally different output. As Figure 9.39 shows, the new ellipse has four different boundary colors.
Like LinearGradientBrush, the PathGradientBrush class provides Blend and InterpolationColors properties. Listing 9.29 shows the InterpolationColors property in use.
FIGURE 9.39: Blending multiple colors
LISTING 9.29: Using the InterpolationColors property of PathGradientBrush
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Public Class Form1
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim g As Graphics = Me.CreateGraphics()
g.Clear(Me.BackColor)
' Create color and points arrays
Dim clrArray As Color() = {Color.Red, Color.Blue, Color.Green, Color.Pink, Color.Yellow, Color.DarkTurquoise}
Dim posArray As Single() = {0.0F, 0.2F, 0.4F, 0.6F, 0.8F, 1.0F}
' Create a ColorBlend object and set its Colors and Positions properties
Dim colorBlend As New ColorBlend()
colorBlend.Colors = clrArray
colorBlend.Positions = posArray
' Set smoothing mode of Graphics object
g.SmoothingMode = SmoothingMode.AntiAlias
' Create a graphics path and add a rectangle
Dim path As New GraphicsPath()
Dim rect As New Rectangle(10, 20, 200, 200)
path.AddRectangle(rect)
' Create a path gradient brush
Dim rgBrush As New PathGradientBrush(path)
' Set Interpolation colors and focus scales
rgBrush.InterpolationColors = colorBlend
rgBrush.FocusScales = New PointF(0.6F, 0.2F)
Dim colors As Color() = {Color.Green}
' Set center and surrounding colors
rgBrush.CenterColor = Color.Red
rgBrush.SurroundColors = colors
' Draw ellipse
g.FillEllipse(rgBrush, rect)
' Dispose of object
g.Dispose()
End Sub
End Class
Figure 9.40 shows the output from Listing 9.29.
You can even apply blending on a path gradient brush using the Blend property. Listing 9.30 creates a Blend object and sets the Blend property of the brush.
FIGURE 9.40: Using the InterpolationColors property of PathGradeintBrush
LISTING 9.30: Using the Blend property of PathGradientBrush
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Public Class Form1
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim g As Graphics = Me.CreateGraphics()
g.Clear(Me.BackColor)
' Create Blend object
Dim blend As New Blend()
' Create point and position arrays
Dim factArray As Single() = {0.0F, 0.3F, 0.5F, 1.0F}
Dim posArray As Single() = {0.0F, 0.2F, 0.6F, 1.0F}
' Set Factors and Positions properties of Blend
blend.Factors = factArray
blend.Positions = posArray
' Set smoothing mode of Graphics object
g.SmoothingMode = SmoothingMode.AntiAlias
' Create path and add a rectangle
Dim path As New GraphicsPath()
Dim rect As New Rectangle(10, 20, 200, 200)
path.AddRectangle(rect)
' Create path gradient brush
Dim rgBrush As New PathGradientBrush(path)
' Set Blend and FocusScales properties
rgBrush.Blend = blend
rgBrush.FocusScales = New PointF(0.6F, 0.2F)
Dim colors As Color() = {Color.Green, Color.Blue, Color.Red, Color.Yellow}
' Set CenterColor and SurroundColors
rgBrush.CenterColor = Color.Red
rgBrush.SurroundColors = colors
g.FillEllipse(rgBrush, rect)
' Dispose of object
g.Dispose()
End Sub
End Class
Figure 9.41 shows the output from Listing 9.30. Blending is done with four different colors.
Just as with LinearGradientBrush, you can use the SetBlendTriangularShape methods with PathGradientBrush.
FIGURE 9.41: Multicolor blending using PathGradientBrush
Conclusion
Hope the article would have helped you in understanding Blending Using PathGradientBrush Objects in GDI+. Read other articles on GDI+ on the website.