Click here to Skip to main content
15,896,063 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.5K   4.4K   61  
How to create a ColorBlend and two color blending UserControls to make it easier.
Imports System.ComponentModel
Imports System.ComponentModel.TypeDescriptor
Imports ColorBlender.cBlenderItems
Imports System.Drawing.Drawing2D


#Region "BlendItemsConverter"

<System.Diagnostics.DebuggerStepThrough()>
Friend Class BlendItemsConverter : Inherits ExpandableObjectConverter

    Public Overrides Function GetCreateInstanceSupported(ByVal context As ITypeDescriptorContext) As Boolean
        Return True
    End Function

    Public Overrides Function CreateInstance(ByVal context As ITypeDescriptorContext, ByVal propertyValues As IDictionary) As Object
        Dim bItem As New cBlenderItems() With
        {
            .cbColor = CType(propertyValues("cbColor"), Color()),
            .cbPosition = CType(propertyValues("cbPosition"), Single()),
            .BorderColor = CType(propertyValues("BorderColor"), Color),
            .FocalPoints = CType(propertyValues("FocalPoints"), cFocalPoints),
            .BlendGradientType = CType(propertyValues("BlendGradientType"), eBlendGradientType),
            .BlendGradientMode = CType(propertyValues("BlendGradientMode"), LinearGradientMode),
            .BlendPathShape = CType(propertyValues("BlendPathShape"), eBlendPathShape)
        }
        Return bItem
    End Function

    Public Overloads Overrides Function CanConvertFrom(ByVal context As ITypeDescriptorContext, ByVal sourceType As System.Type) As Boolean
        If (sourceType Is GetType(String)) Then
            Return True
        End If
        Return MyBase.CanConvertFrom(context, sourceType)
    End Function

    Public Overloads Overrides Function ConvertFrom(ByVal context As ITypeDescriptorContext, _
      ByVal culture As System.Globalization.CultureInfo, ByVal value As Object) As Object

        'cONVERT THE 

        If TypeOf value Is String Then
            Dim s As String() = Split(CType(value, String), "|")
            Try

                Dim bColors As New List(Of Color)
                Dim bPoints As New List(Of Single)
                For Each cstring As String In Split(s(1), ";")
                    bColors.Add(GetConverter(GetType(Color)).ConvertFromString(cstring))
                Next
                For Each cstring As String In Split(s(2), ";")
                    bPoints.Add(GetConverter(GetType(Single)).ConvertFromString(cstring))
                Next

                Return New cBlenderItems(bColors.ToArray, bPoints.ToArray,
                   GetConverter(GetType(Color)).ConvertFromString(s(0)),
                   GetConverter(GetType(cFocalPoints)).ConvertFromString(s(3)),
                   GetConverter(GetType(eBlendGradientType)).ConvertFromString(s(4)),
                   GetConverter(GetType(LinearGradientMode)).ConvertFromString(s(5)),
                   GetConverter(GetType(eBlendPathShape)).ConvertFromString(s(6))
                )

            Catch ex As Exception
                Throw New ArgumentException(String.Format(
                "Can not convert '{0}' to type cBlenderItems {1}",
                CStr(value),
                ex.Message))
            End Try
        Else
            Return New cBlenderItems()
        End If
        Return MyBase.ConvertFrom(context, culture, value)
    End Function

#Region "Test"
    Public Overloads Overrides Function ConvertTo(ByVal context As ITypeDescriptorContext, _
      ByVal culture As System.Globalization.CultureInfo, _
      ByVal value As Object, ByVal destinationType As Type) As Object

        If (destinationType Is GetType(String) AndAlso TypeOf value Is cBlenderItems) Then
            Return CType(value, cBlenderItems).ToString
        End If
        Return MyBase.ConvertTo(context, culture, value, destinationType)

    End Function
#End Region


End Class

#End Region

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