【问题标题】:Convert hex color string to RGB color将十六进制颜色字符串转换为 RGB 颜色
【发布时间】:2012-11-01 15:08:06
【问题描述】:

我想将十六进制颜色转换为 RGB 颜色。

我使用了以下代码:

Me.BackColor = RGB("#000000")

然后它会抛出以下异常:

Argument not specified for parameter 'Green' of 'Public Function RGB(Red As Integer, Green As Integer, Blue As Integer) As Integer'

正确的做法是什么?

【问题讨论】:

    标签: vb.net winforms


    【解决方案1】:

    通过颜色翻译:

    ColorTranslator.FromHtml("#003399") 
    

    其他方式:

    Public Function ConvertToRbg(ByVal HexColor As String) As Color
        Dim Red As String
        Dim Green As String
        Dim Blue As String
        HexColor = Replace(HexColor, "#", "")
        Red = Val("&H" & Mid(HexColor, 1, 2))
        Green = Val("&H" & Mid(HexColor, 3, 2))
        Blue = Val("&H" & Mid(HexColor, 5, 2))
        Return Color.FromArgb(Red, Green, Blue)
    End Function
    

    或:

    Public Shared Function HexToColor(ByVal hexColor As String) As Color
        If hexColor.IndexOf("#"c) <> -1 Then
            hexColor = hexColor.Replace("#", "")
        End If
        Dim red As Integer = 0
        Dim green As Integer = 0
        Dim blue As Integer = 0
        If hexColor.Length = 6 Then
            red = Integer.Parse(hexColor.Substring(0, 2), NumberStyles.AllowHexSpecifier)
            green = Integer.Parse(hexColor.Substring(2, 2), NumberStyles.AllowHexSpecifier)
            blue = Integer.Parse(hexColor.Substring(4, 2), NumberStyles.AllowHexSpecifier)
        ElseIf hexColor.Length = 3 Then
            red = Integer.Parse(hexColor(0).ToString() + hexColor(0).ToString(), NumberStyles.AllowHexSpecifier)
            green = Integer.Parse(hexColor(1).ToString() + hexColor(1).ToString(), NumberStyles.AllowHexSpecifier)
            blue = Integer.Parse(hexColor(2).ToString() + hexColor(2).ToString(), NumberStyles.AllowHexSpecifier)
        End If
        Return Color.FromArgb(red, green, blue)
    End Function
    

    或:

        Dim c As String = "#ffffff"
        c = Replace(c, "#", "")
        c = "&H" & c
        ColorTranslator.FromOle(c)
    

    或:

    Public Function hexToRbgNew(ByVal Hex As String) As Color
        Hex = Replace(Hex, "#", "")
        Dim red As String = "&H" & Hex.Substring(0, 2)
        Hex = Replace(Hex, red, "", , 1)
        Dim green As String = "&H" & Hex.Substring(0, 2)
        Hex = Replace(Hex, green, "", , 1)
        Dim blue As String = "&H" & Hex.Substring(0, 2)
        Hex = Replace(Hex, blue, "", , 1)
        Return Color.FromArgb(red, green, blue)
    End Function
    

    【讨论】:

      【解决方案2】:

      这对我有用:(vb.Net)

      cell.BackColor = Drawing.Color.FromArgb(&H3399FF)
      

      【讨论】:

        猜你喜欢
        • 2019-01-01
        • 2011-04-06
        • 2012-07-25
        • 2014-04-25
        • 2022-08-10
        • 2012-11-24
        • 2020-09-28
        • 1970-01-01
        • 2011-07-30
        相关资源
        最近更新 更多