如何使用C#在字典中添加颜色 [英] How to add colors in dictionary using C#
本文介绍了如何使用C#在字典中添加颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在字典中添加颜色。
例如
如果键是红色 那么它应该有RGB格式的值。
怎么做
I want to add colors in dictionary.
Fro example
If key is "Red" then it should have values in RGB format.
How to do so
<pre lang="c#">
Dictionary<string, string> ColorInformation = new Dictionary<string, string>();
string colorOrange = "Orange";
Color Orange = Color.FromName(colorOrange);
string colorOrangeValue = "R=" + Orange.R + "," + "G=" + Orange.G + "," + "B=" + Orange.B;
ColorInformation.Add("Orange", colorOrangeValue);
</pre>
I have given the values in string format.
Any other solution for this
推荐答案
// required:
using System.Drawing;
private Dictionary<string, Color> ColorDictionary = new Dictionary<string, Color>();
private void AddColor(string colorName)
{
Color colorCandidate = Color.FromName(colorName);
if (colorCandidate.ToArgb() == 0) throw new ArgumentException("Error: No color named:" + colorName + " exists");
ColorDictionary.Add(colorName, colorCandidate);
}
此示例仅处理System.Drawing.Color中定义的Color Enumeration。
如果使用'FromName如果方法的参数不是有效的Color名称,则返回#0的结果。在此示例中,如果Color名称无效,则会引发错误。示例:
This example only deals with the Color Enumeration defined in System.Drawing.Color.
If you use the 'FromName method with an argument that's not a valid Color name, it will return a result of #0. In this example an error is thrown if the Color name isn't valid. Example:
// somewhere in a method or EventHandler
// add some Color to the Dictionary
AddColor("Red");
AddColor("Black");
// somewhere in a method or EventHandler
// use a Color in the Dictionary
panel1.BackColor = ColorDictionary["Red"];
继续有了这个 。希望它有所帮助..
字典<字符串,> ColorInformation = new Dictionary< string,>();
string color =White;
颜色白= Color.FromName(颜色);
//颜色将具有适当的RGB值。
Console.WriteLine(white.R);
Console.WriteLine(white.G);
Console.WriteLine(white.B);
Proceed with this . Hope it helps..
Dictionary<string,> ColorInformation = new Dictionary<string,>();
string color="White";
Color white = Color.FromName(color);
// Color will have appropriate R G B values.
Console.WriteLine(white.R);
Console.WriteLine(white.G);
Console.WriteLine(white.B);
这篇关于如何使用C#在字典中添加颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文