轉.色碼指定問題

摘要:轉.色碼指定問題

之前要將DataGrid裡的資料"美化"成設計人員給的色碼時,只能用"接近"的顏色,像Color.AliceBlue

但,在論壇上看到可以用"#000000"色碼的文章!如下:

原文:http://forums.microsoft.com/MSDN-CHT/ShowPost.aspx?PostID=3116786&SiteID=14

chhuang

例如:  Color.AliceBlue 轉成色碼嗎?

Color c = Color.AliceBlue;
string cRGB = string.Format("#{0:X2}{1:X2}{2:X2}", c.R, c.G, c.B);

色碼轉 Color
string strColor = "#F0F8FF";

MatchCollection mc = Regex.Matches(strColor, @"([0-9A-Fa-f]{2})");

 

Color c = new Color();

if (mc.Count == 3)

{

    int r = Convert.ToInt32(mc[0].Groups[0].Value, 16);

    int g = Convert.ToInt32(mc[1].Groups[0].Value, 16);

    int b = Convert.ToInt32(mc[2].Groups[0].Value, 16);

    c = Color.FromArgb(r, g, b);

}


p u m a

有一個不錯用的類別...ColorTranslator...有兩個方法..FromHtml,ToHtml

Color c = ColorTranslator.FromHtml("#fff000");
Response.Write(c.R);
Response.Write(ColorTranslator.ToHtml(c));