"#RRGGBB"与Color类型的转换 补充
"#RRGGBB"与Color类型的转换,ColorConverter和WebColorConverter之间的差别一文介绍了一般的转换方法。
如果你的颜色是WEB安全色等named color,Converter把Color转成String的时候会把它转换成LightGray之类的字符串,而不是"#D3D3D3",用以下这个简单的函数就能解决这个问题了。
static string Color2WebColorString(Color c)
{
int r = (int)c.R;
int g = (int)c.G;
int b = (int)c.B;
string rs = r.ToString();
if(r<10)
rs = "0" +rs;
string gs = g.ToString();
if(g <10)
gs = "0"+ gs;
string bs = b.ToString();
if(b < 10)
bs = "0" + bs;
return rs+gs+bs;
}
{
int r = (int)c.R;
int g = (int)c.G;
int b = (int)c.B;
string rs = r.ToString();
if(r<10)
rs = "0" +rs;
string gs = g.ToString();
if(g <10)
gs = "0"+ gs;
string bs = b.ToString();
if(b < 10)
bs = "0" + bs;
return rs+gs+bs;
}