【问题标题】:Color.fromArgb dosen't accept integerColor.fromArgb 不接受整数
【发布时间】:2016-11-11 11:35:08
【问题描述】:

在 Visual Basic 2015 中,函数 Color.fromArgb 不能接受函数 Color.toArgb 的整数 它只能接受以下内容:

FromArgb(a As Byte, r As Byte, g As Byte, b As Byte) As Color

我将函数 Color.toArgb 中的整数从颜色对话框保存到数据库。当我尝试通过函数 Color.fromArgb 加载和使用它时,它不接受整数作为参数。

ColorDialog1.ShowDialog()
TextBox1.Text = ColorDialog1.Color.ToArgb
TextBox1.ForeColor = ColorDialog1.Color

那我就记起来了

TextBox2.ForeColor = Color.FromArgb(TextBox1.Text)

它给出了错误。

有没有简单的方法来解决这个问题?

【问题讨论】:

  • 您能给我们看一些带有实际数字的代码,以便我们尝试重现问题吗?
  • 有一个使用 argb 值的重载:Function FromArgb(argb As Integer)
  • 我通过函数 Color.toArgb 从颜色对话框中获取一个整数,然后将其保存到数据库中。当我尝试通过函数 Color.fromArgb 加载和使用它时,它不接受整数作为参数。
  • 这些解决方案对您有帮助吗?

标签: vb.net colors


【解决方案1】:

TextBox1.Text 将返回一个字符串。首先将其转换为整数。详情见这篇MSDN文章:

Dim number As Integer = Int32.Parse(TextBox1.Text)

然后将数字传入FromArgb():

FromArgb() 被重载。根据MSDN,你也可以传入整数:

SolidBrush red = new SolidBrush(Color.FromArgb(120, 255, 0, 0));

如果您正在寻找更简单的选择,您可以执行以下操作:

int transparency = 75;
var transparentColor = Color.FromArgb(transparency, Color.Red);

【讨论】:

  • 我保存的整数是 -16744448 不是 3 个数字。
  • 问题似乎是您将错误的类型传递给 Color.FromArgb()。已更新答案以反映这一点。
【解决方案2】:

这是一个支持 Plutonix 答案的代码示例

Color.FromArgb(CInt(red_txt.Text), CInt(green_txt.Text), CInt(blue_txt.Text))

【讨论】:

  • 我保存的整数是 -16744448 不是 3 个数字。
【解决方案3】:

我通过使用从 Color.FromArgb 转换整数的函数解决了这个问题

Function IntegerToColor(ByRef RGB As Int32) As Color
    Dim Bytes As Byte() = BitConverter.GetBytes(RGB)
    Dim Alpha As Byte = Bytes(3)
    Dim Red As Byte = Bytes(2)
    Dim Green As Byte = Bytes(1)
    Dim Blue As Byte = Bytes(0)
    Return Color.FromArgb(Alpha, Red, Green, Blue)
End Function

我从另一个答案中解决了这个问题,它对我有用

【讨论】:

    【解决方案4】:

    如果不起作用,请删除 alpha 参数

    Public Shared Function IntegerToColor(ByRef RGB As Int32) As Color
        Dim Bytes As Byte() = BitConverter.GetBytes(RGB)
        'Dim Alpha As Byte = Bytes(3)
        Dim Red As Byte = Bytes(2)
        Dim Green As Byte = Bytes(1)
        Dim Blue As Byte = Bytes(0)
        'Return Color.FromArgb(Alpha, Red, Green, Blue)
        Return Color.FromArgb(Red, Green, Blue)
    End Function
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-11
      • 2018-08-22
      • 2019-07-24
      • 1970-01-01
      • 2011-07-31
      • 1970-01-01
      相关资源
      最近更新 更多