Wpf 从System.Windows.Media.color中提取颜色名称

Wpf 从System.Windows.Media.color中提取颜色名称,wpf,Wpf,如何从System.windows.Media.color对象中提取颜色(例如“绿色”)的名称?.tostring()方法为我提供十六进制格式#ff008000 在WPF中,十六进制代码与rgba一样 #ff008000 会是 rgba(255, 0, 80, 0); // last 2 00s are for alpha filter. 如果结果是这样的话。您应该使用switch语句将其转换为一些字符串值。另外,.ToString()方法不会生成人类可读的字符串结果,如绿色。它只是将结果转

如何从System.windows.Media.color对象中提取颜色(例如“绿色”)的名称?
.tostring()
方法为我提供十六进制格式#ff008000

在WPF中,十六进制代码与rgba一样

#ff008000
会是

rgba(255, 0, 80, 0); // last 2 00s are for alpha filter.
如果结果是这样的话。您应该使用switch语句将其转换为一些字符串值。另外,
.ToString()
方法不会生成人类可读的字符串结果,如
绿色
。它只是将结果转换为字符串,同时将值传递给需要字符串参数的方法和函数

下面的代码可以为您实现这一点:

var converter = new System.Windows.Media.BrushConverter();
var brush = (Brush) converter.ConvertFromString("#ff008000");

现在使用
画笔

在WPF中,十六进制代码与rgba一样

#ff008000
会是

rgba(255, 0, 80, 0); // last 2 00s are for alpha filter.
如果结果是这样的话。您应该使用switch语句将其转换为一些字符串值。另外,
.ToString()
方法不会生成人类可读的字符串结果,如
绿色
。它只是将结果转换为字符串,同时将值传递给需要字符串参数的方法和函数

下面的代码可以为您实现这一点:

var converter = new System.Windows.Media.BrushConverter();
var brush = (Brush) converter.ConvertFromString("#ff008000");

现在使用
画笔

您可以使用反射来获得颜色名称:

static string GetColorName(Color col)
{
    PropertyInfo colorProperty = typeof(Colors).GetProperties()
        .FirstOrDefault(p => Color.AreClose((Color)p.GetValue(null), col));
    return colorProperty != null ? colorProperty.Name : "unnamed color";
}
以下代码显示了如何使用
GetColorName()


请注意,上面的
GetColorName()
方法不是很快,因为它使用反射。如果您计划多次调用
GetColorName()
,可能应该将颜色表缓存在字典中。

您可以使用反射来获取颜色名称:

static string GetColorName(Color col)
{
    PropertyInfo colorProperty = typeof(Colors).GetProperties()
        .FirstOrDefault(p => Color.AreClose((Color)p.GetValue(null), col));
    return colorProperty != null ? colorProperty.Name : "unnamed color";
}
以下代码显示了如何使用
GetColorName()


请注意,上面的
GetColorName()
方法不是很快,因为它使用反射。如果计划多次调用
GetColorName()
,则可能应该将颜色表缓存在字典中。

我的Visual Basic翻译如下:

   Function GetColorName(ByVal col As System.Windows.Media.Color) As String

    Dim coltype As System.Type = GetType(System.Windows.Media.Colors)
    Dim colproplist() As PropertyInfo = coltype.GetProperties

    Try

        Dim colorproperty As PropertyInfo = colproplist.FirstOrDefault(Function(p As PropertyInfo) Color.AreClose(p.GetValue(col, Nothing), col))

        Return colorproperty.Name

    Catch ex As Exception

        Return ("unnamed color")

    End Try

End Function

当使用未命名的颜色执行此函数时,我必须捕获nullreference异常。为什么,我不知道。

我的Visual Basic翻译如下:

   Function GetColorName(ByVal col As System.Windows.Media.Color) As String

    Dim coltype As System.Type = GetType(System.Windows.Media.Colors)
    Dim colproplist() As PropertyInfo = coltype.GetProperties

    Try

        Dim colorproperty As PropertyInfo = colproplist.FirstOrDefault(Function(p As PropertyInfo) Color.AreClose(p.GetValue(col, Nothing), col))

        Return colorproperty.Name

    Catch ex As Exception

        Return ("unnamed color")

    End Try

End Function

当使用未命名的颜色执行此函数时,我必须捕获nullreference异常。为什么,我不知道。

如果定义了英文颜色名称,它将返回英文颜色名称:

Function GetName(color As Media.Color) As String
  Dim c = System.Drawing.Color.FromArgb(color.A, color.R, color.G, color.B)
  Return System.Drawing.ColorTranslator.ToHtml(c)
End Function

如果定义了英文颜色名称,则返回英文颜色名称:

Function GetName(color As Media.Color) As String
  Dim c = System.Drawing.Color.FromArgb(color.A, color.R, color.G, color.B)
  Return System.Drawing.ColorTranslator.ToHtml(c)
End Function

谢谢当我“说”VB时,我不得不将C#翻译成VB,效果很好:哦,呵呵,我从来不知道你在用VB工作。很抱歉谢谢当我“说”VB时,我不得不将C#翻译成VB,效果很好:哦,呵呵,我从来不知道你在用VB工作。很抱歉谢谢成功了!明天我会把你的答案翻译成VB!谢谢成功了!明天我会把你的答案翻译成VB!可能有兴趣。可能有兴趣。