How to convert color name to the corresponding hexadecimal representation?

前端 未结 5 1386
面向向阳花
面向向阳花 2020-12-19 13:56

For example:

blue 

converts to:

#0000FF

I wrote it as:

Color color = Color.FromName(\"blue\

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-19 14:20

    You're half way there. Use .ToArgb to convert it to it's numberical value, then format it as a hex value.

    int ColorValue = Color.FromName("blue").ToArgb();
    string ColorHex = string.Format("{0:x6}", ColorValue);
    

提交回复
热议问题