Click here to Skip to main content
15,898,134 members
Articles / Multimedia / GDI+

ColorBlender - Dynamic Gradient Color Blend Creation Control (VB.NET)

Rate me:
Please Sign up or sign in to vote.
4.90/5 (29 votes)
1 Aug 2012CPOL6 min read 89.6K   4.4K   61  
How to create a ColorBlend and two color blending UserControls to make it easier.

Imports System.Drawing.Drawing2D
Imports ColorBlender
Imports ColorBlender.cBlenderItems

Public Class TestForm

    '    'gColorBlend Properties and Methods:
    '    '.BlendGradientMode
    '    '.BlendGradientType
    '    '.BlendPathShape
    '    '.BorderColor
    '    '.cbColor
    '    '.cbPosition
    '    '.FocalPoints
    '    '.GetColorBlendForBrush()

    Sub BuildBlend()

        With GColorBlender1.gColorBlend

            'Display the Color Blend and information for the gColorBlender in a Listview
            ListView1.Items.Clear()
            For i As Integer = 0 To .cbColor.Length - 1
                Dim lstItem As New ListViewItem
                lstItem.Text = Math.Round(.cbPosition(i), 2)
                lstItem.SubItems.Add(.cbColor(i).GetColorBestName())
                lstItem.SubItems.Add(.cbColor(i).GetColorBestName(True))
                lstItem.SubItems.Add(.cbColor(i).A)
                lstItem.SubItems.Add(.cbColor(i).R)
                lstItem.SubItems.Add(.cbColor(i).G)
                lstItem.SubItems.Add(.cbColor(i).B)

                ListView1.Items.Add(lstItem)
            Next

            'Show Center Point for the Demo Blend
            If .BlendGradientType = ColorBlender.cBlenderItems.eBlendGradientType.Linear Then
                txbCenterPoint.Text = ""
                txbFocusScales.Text = ""
            Else
                Dim cpt As PointF = GColorBlender1.BlendConvertCenterPoint(picDemoBlend.Width, picDemoBlend.Height)
                txbCenterPoint.Text = String.Format("X: {0:0.00}, Y: {1:0.00}", cpt.X, cpt.Y)
                txbFocusScales.Text = String.Format("X: {0}, Y: {1}",
                                                    GColorBlender1.gColorBlend.FocalPoints.FocusPtX,
                                                    GColorBlender1.gColorBlend.FocalPoints.FocusPtY)
            End If

            ' How to Use the gColorBlender Properties to brush the graphics object with a blend
            'Create a Graphics Object as the canvas to paint the blend
            Using bitmapBuffer As Bitmap = _
                New Bitmap(picDemoBlend.Width, picDemoBlend.Height),
                 g As Graphics = Graphics.FromImage(bitmapBuffer)
                g.SmoothingMode = SmoothingMode.AntiAlias

                Dim rect As Rectangle = New Rectangle(-1, -1, picDemoBlend.Width + 1, picDemoBlend.Height + 1)

                'Use a TextureBrush to tile the background image over the Panel
                Using tbr As TextureBrush = New TextureBrush(picBackgroundtile.Image.Clone, WrapMode.Tile)
                    g.FillRectangle(tbr, 0, 0, rect.Width, rect.Height)
                End Using

                'Create a LinearGradientBrush or PathGradientBrush depending on 
                'the BlendGradientMode, BlendGradientType, and BlendPathShape choice
                Using gp As New GraphicsPath
                    Dim br As Brush
                    If .BlendGradientType = eBlendGradientType.Linear Then
                        gp.AddRectangle(rect)
                        br = New LinearGradientBrush(rect, Color.White, Color.Black,
                                                     .BlendGradientMode) With {
                                                    .InterpolationColors = GColorBlender1.GetColorBlendForBrush}
                        g.FillRectangle(br, rect)

                    Else
                        gp.AddPath(GColorBlender1.GetShapePath(rect), False)

                        br = New PathGradientBrush(gp) With {
                            .CenterPoint = GColorBlender1.BlendConvertCenterPoint(picDemoBlend.Width,
                                                                                  picDemoBlend.Height),
                            .FocusScales = GColorBlender1.gColorBlend.FocalPoints.FocusScales,
                           .InterpolationColors = GColorBlender1.GetColorBlendForBrush}
                        g.FillPath(br, gp)

                    End If
                    Using pn As Pen = New Pen(.BorderColor, 4) With {
                                                        .Alignment = PenAlignment.Inset}
                        g.DrawPath(pn, gp)
                    End Using
                    br.Dispose()
                End Using

                picDemoBlend.Image = bitmapBuffer.Clone

            End Using
        End With

    End Sub

    Sub BuildBlend2()

        ' How to Use the gColorBlender Properties to brush the graphics object
        Using bitmapBuffer As Bitmap = New Bitmap(picDemoBlend2.Width, picDemoBlend2.Height),
             g As Graphics = Graphics.FromImage(bitmapBuffer)

            g.SmoothingMode = SmoothingMode.AntiAlias
            Dim rect As Rectangle = New Rectangle(-1, -1, picDemoBlend2.Width + 1, picDemoBlend2.Height + 1)

            'Use a TextureBrush to tile the image over the Panel
            Using tbr As TextureBrush = New TextureBrush(picBackgroundtile.Image.Clone, WrapMode.Tile)
                g.FillRectangle(tbr, 0, 0, rect.Width, rect.Height)
            End Using

            ' Create a LinearGradientBrush
            Dim br As New LinearGradientBrush(rect, Color.White, Color.Black,
                                    LinearGradientMode.Vertical) With {
                                     .InterpolationColors = GColorBlender2.GetColorBlendForBrush}
            g.FillRectangle(br, rect)
            br.Dispose()

            picDemoBlend2.Image = bitmapBuffer.Clone

        End Using
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Button1.Tag = True
    End Sub

    Private Sub GColorBlender1_BlendChanged() Handles GColorBlender1.BlendChanged
        BuildBlend()
    End Sub

    Private Sub GColorBlender2_BlendChanged() Handles GColorBlender2.BlendChanged
        BuildBlend2()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Button1.Tag = Not Button1.Tag
        For Each c As Control In grpBuildABlend.Controls
            If TypeOf (c) Is Label OrElse TypeOf (c) Is gArrow Then
                c.Visible = CType(Button1.Tag, Boolean)
            End If
        Next
    End Sub

    Private Sub panRainbow_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles panRainbow.Paint

        'Simple Breakdown of creating a ColorBlend from scratch
        Dim g As Graphics = e.Graphics
        g.SmoothingMode = SmoothingMode.AntiAlias

        Dim rect As Rectangle = New Rectangle(0, 0, panRainbow.Width, panRainbow.Height)
        Dim blend As ColorBlend = New ColorBlend()

        'Add the Array of Color
        Dim bColors As Color() = New Color() { _
            Color.Red, _
            Color.Yellow, _
            Color.Lime, _
            Color.Cyan, _
            Color.Blue, _
            Color.Violet}
        blend.Colors = bColors

        'Add the Array Single (0-1) colorpoints to place each Color
        Dim bPts As Single() = New Single() { _
            0, _
            0.327, _
            0.439, _
            0.61, _
            0.777, _
            1}
        blend.Positions = bPts

        ' Create a LinearGradientBrush or PathGradientBrush depending on the BlendGradientType choice
        Using br As New LinearGradientBrush(rect, Color.White, Color.Black, LinearGradientMode.Horizontal)

            'Blend the colors into the Brush
            br.InterpolationColors = blend

            'Fill the rect with the blend
            g.FillRectangle(br, rect)

        End Using

    End Sub

    Private Sub lblClear_Click(sender As System.Object, e As System.EventArgs) Handles lblClear.Click
        GColorBlender2.ResetgColorBlend()
    End Sub

    Private Sub lblReset_Click(sender As System.Object, e As System.EventArgs) Handles lblReset.Click
        GColorBlender2.gColorBlend = System.ComponentModel.TypeDescriptor.GetConverter(GetType(cBlenderItems)).ConvertFromString("Black|255,255,255,192;255,255,255,192;Navy;255,192,192,255;Navy;255,255,255,192;120,255,255,192;255,192,192,255;Maroon;Red;Red;Maroon;255,192,192,255;255,192,192,255|0;0.065;0.084;0.103;0.146;0.153;0.435;0.698;0.717;0.744;0.756;0.820;0.828;1|0.5, 0.5, 0, 0|Linear|Vertical|Rectangle")

    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
I first got hooked on programing with the TI994A. After it finally lost all support I reluctantly moved to the Apple IIe. Thank You BeagleBros for getting me through. I wrote programs for my Scuba buisness during this time. Currently I am a Database manager and software developer. I started with VBA and VB6 and now having fun with VB.NET/WPF/C#...

Comments and Discussions