简体   繁体   中英

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.

In general, it can be obtained from the Name property of the System.Drawing.Color struct:

using System.Drawing;

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

output: 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

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:

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

output: ffff0000

but I found another solution that can be used to get ARGB value as Hex code (see the answer below).

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:

using System.Drawing;

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

output: ffff0000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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