I have a problem with getting the name of a color from a colordialog box in C#. I now have the following code to accomplish this:
private void bPickColor_Click(object sender, EventArgs e)
{
ColorDialog colorDlg = new ColorDialog();
colorDlg.AllowFullOpen = false;
colorDlg.AnyColor = true;
colorDlg.SolidColorOnly = false;
colorDlg.Color = Color.Red;
if (colorDlg.ShowDialog() == DialogResult.OK)
{
//ColorConverter conv = new ColorConverter();
Color c = colorDlg.Color;
//string s = conv.ConvertToString(c);
//string h = Conversion.Hex(s.ToArgb);
_ColorName = c.Name.ToString();
MessageBox.Show(_ColorName);
bPickColor.BackColor = colorDlg.Color;
}
}
As you can see I use 'C.Name.ToString' to get the color name, but somehow this gives back the color code like for example: f0018000. What I need is the name of the color like for example: RED, or BLUE.
The reason I need this is because I store the colors in a datatable
and read them using this code:
Color.FromName((string)dtrow["color"]);
So what I've tried so far is to search for code that can get the color name based on the color code, but the pieces of code I found didn't work.
Now I also tried to make a dictionary of all color codes with the corresponding color names, but I couldn't make this to work either.
Does anybody has a solution for this problem? How can I get the name (Blue, Red) of the color?