Adding Multicolor Support to Gradients in VB.NET

In this article I will explain about Adding Multicolor Support to Gradients in GDI+.
  • 2649
 

So far in this section, we have been using only two colors (the default supported by LinearGradientBrush). What if we want to use more than two colors? No problem!

The LinearGradientBrush class provides properties that are useful for blending. Two of these properties are InterpolationColors and Blend. The Blend property is represented by the Blend object, and InterpolationColors is represented by the ColorBlend object. To apply multicolor gradients, simply create Blend and ColorBlend objects, attach these objects to a LinearGradientBrush object, and use the brush to fill shapes.

Listing 9.25 creates a ColorBlend object, sets its Colors and Positions properties, and sets the InterpolationColors property of the brush.

LISTING 9.25: Using the InterpolationColors property of LinearGradientBrush

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 a LinearGradientBrush object
        Dim brBrush As New LinearGradientBrush(New Point(0, 0), New Point(50, 20), Color.Blue, Color.Red)
        Dim rect As New Rectangle(20, 20, 200, 100)

        ' 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 InterpolationColors property
        brBrush.InterpolationColors = colorBlend

        ' Draw shapes
        g.FillRectangle(brBrush, rect)
        rect.Y = 150
        rect.Width = 100
        rect.Height = 100
        g.FillEllipse(brBrush, rect)

        ' Dispose of object
        g.Dispose()
    End Sub
End Class

Figure 9.35 shows the output from Listing 9.25. The gradient has multiple colors

FIGURE-9_35.gif

FIGURE 9.35: A multicolor gradient

The Blend property of LinearGradientBrush allows you to attach a Blend object to the brush, which represents the positions and facts of the blend. Listing 9.26 creates a Blend object and sets its Factors and Positions properties, as well the Blend property of the brush.

LISTING 9.26: Using the Blend property of LinearGradientBrush

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 a linear gradient brush
        Dim brBrush As New LinearGradientBrush(New Point(0, 0), New Point(50, 20), Color.Blue, Color.Red)

        ' Create a Blend object
        Dim blend As New Blend()
        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 Blend's Factors and Positions properties
        blend.Factors = factArray
        blend.Positions = posArray

        ' Set Blend property of the brush
        brBrush.Blend = blend

        ' Fill a rectangle and an ellipse
        g.FillRectangle(brBrush, 10, 20, 200, 100)
        g.FillEllipse(brBrush, 10, 150, 120, 120)

        ' dispose of object
        g.Dispose()
    End Sub
End Class

Figure 9.36 shows the output from Listing 9.26. The blend's position and colors are controlled by the Factors property.

FIGURE-9_36.gif

FIGURE 9.36: Using blending in a linear gradient brush

Conclusion

Hope the article would have helped you in understanding Adding Multicolor Support to Gradients in GDI+. Read other articles on GDI+ on the website.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.