C#刷串 [英] C# Brush to string

查看:118
本文介绍了C#刷串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我寻找一种方式来保存毛笔作为字符串的颜色。
比如我有一个有红色的画笔。
现在我想在一个文本框写红。

I search a way to save the color of a brush as a string. For example I have a Brush which has the color red. Now I want to write "red" in a textbox.

感谢您的帮助。

推荐答案

如果在使用颜色创建的System.Drawing.Color ,那么你可以使用颜色的的 名称 属性。

If the Brush was created using a Color from System.Drawing.Color, then you can use the Color's Name property.

另外,你可以只尝试使用查找颜色反射

Otherwise, you could just try to look up the color using reflection

// hack
var b = new SolidBrush(System.Drawing.Color.FromArgb(255, 255, 235, 205));
var colorname = (from p in typeof(System.Drawing.Color).GetProperties()
                 where p.PropertyType.Equals(typeof(System.Drawing.Color))
                 let value = (System.Drawing.Color)p.GetValue(null, null)
                 where value.R == b.Color.R &&
                       value.G == b.Color.G &&
                       value.B == b.Color.B &&
                       value.A == b.Color.A
                 select p.Name).DefaultIfEmpty("unknown").First();

// colorname == "BlanchedAlmond"

或自己创建一个映射,可能使用的词典看颜色上)=htt​​p://gucky.uni-muenster.de/cgi-bin/rgbtab-en 相对=nofollow>各地的许多颜色表中的一个

or create a mapping yourself (and look the color up via a Dictionary), probably using one of many color tables around.

编辑:

您写了评论说你使用 System.Windows.Media.Color ,但你仍然可以使用 System.Drawing中。颜色来查找颜色的名称。

You wrote a comment saying you use System.Windows.Media.Color, but you could still use System.Drawing.Color to look up the name of the color.

var b = System.Windows.Media.Color.FromArgb(255, 255, 235, 205);
var colorname = (from p in typeof(System.Drawing.Color).GetProperties()
                 where p.PropertyType.Equals(typeof(System.Drawing.Color))
                 let value = (System.Drawing.Color)p.GetValue(null, null)
                 where value.R == b.R &&
                       value.G == b.G &&
                       value.B == b.B &&
                       value.A == b.A
                 select p.Name).DefaultIfEmpty("unknown").First();

这篇关于C#刷串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆