[英]Trouble getting color c#
I have this code: 我有以下代码:
int converted = Convert.ToInt32(value);
string hexValue = converted.ToString("X");
Color color = System.Drawing.ColorTranslator.FromHtml("#" + hexValue);
return color;
That get strings like "12222222" and converts them to C# colors. 那将得到像“ 12222222”这样的字符串,并将它们转换为C#颜色。 But I'm getting this string "255" and I don't know how to handle this.
但是我得到的字符串是“ 255”,我不知道该如何处理。 Can anyone shed some light on this strange number?
任何人都可以对这个奇怪的数字有所了解吗?
Thanks 谢谢
Update Forgot to mention: in a demo project his function works with value="255". 更新忘记提及:在演示项目中,他的功能使用value =“ 255”。 In my project - doenst.
在我的项目 - doenst。
try 尝试
string hexValue = converted.ToString("X8");
that will ensure that there are 8 characters in the hexValue 这将确保hexValue中有8个字符
If you want to convert an integer ( int
or Int32
) to a Color
you can simply use: 如果要将整数 (
int
或Int32
)转换为Color
,则可以简单地使用:
// value is an integer in this case
Color color = Color.FromArgb(value);
No need to convert it to a hex string. 无需将其转换为十六进制字符串。
If you want to convert a decimal String
to a Color
just use: 如果要将十进制
String
转换为Color
,请使用:
int value = Int32.Parse(decimalString);
Color color = Color.FromArgb(value);
if you want to convert a hexadecimal String
to a Color
: 如果要将十六进制
String
转换为Color
:
int value = Int32.Parse(hexString, NumberStyles.HexNumber); // System.Globalization
Color color = Color.FromArgb(value);
There is a converter in the framework. 框架中有一个转换器。
From this question : 从这个问题 :
System.Windows.Media.ColorConverter System.Windows.Media.ColorConverter
var color = (Color)ColorConverter.ConvertFromString("#FF010203");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.