简体   繁体   English

获取 System.Drawing.Color 值作为 ARGB 十六进制代码

[英]Get System.Drawing.Color value as ARGB Hex code

I want to get System.Drawing.Color value in ARGB Hex format, eg Color.Red as ffff0000 in an easy way.我想以一种简单的方式获得 ARGB 十六进制格式的System.Drawing.Color值,例如Color.Red作为ffff0000

In general, it can be obtained from the Name property of the System.Drawing.Color struct:一般来说,它可以从System.Drawing.Color结构体的Name属性中获取:

using System.Drawing;

var color = ColorTranslator.FromHtml("#ff0000");
Console.WriteLine(color.Name);

output: ffff0000输出: ffff0000

But there is a problem if the value is provided as known color :但是,如果该值以已知颜色的形式提供,则会出现问题:

using System.Drawing;

var color = Color.Red;
Console.WriteLine(color.Name);

output: Red输出: Red

I applied the solution from this question to convert System.Drawing.Color value to RGB code and I prefixed the code with ff for alpha channel:我应用了这个问题的解决方案将System.Drawing.Color值转换为 RGB 代码,并在代码前加上ff表示 alpha 通道:

var c = System.Drawing.Color.Red;
Console.WriteLine("ff" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2"));

output: ffff0000输出: ffff0000

but I found another solution that can be used to get ARGB value as Hex code (see the answer below).但我找到了另一种可用于将 ARGB 值作为十六进制代码获取的解决方案(请参阅下面的答案)。

The ARGB Hex code may be obtained from the Name property for known colors by converting it to ARGB and back to System.Drawing.Color struct: ARGB 十六进制代码可以从已知颜色Name属性中获取,方法是将其转换为 ARGB 并返回到System.Drawing.Color结构:

using System.Drawing;

var color = Color.Red;
color = Color.FromArgb(color.ToArgb());
Console.WriteLine(color.Name);

output: ffff0000输出: ffff0000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM