-1

I want to get System.Drawing.Color value in ARGB Hex format, e.g. 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).

FIL
  • 1,148
  • 3
  • 15
  • 27

1 Answers1

0

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

FIL
  • 1,148
  • 3
  • 15
  • 27
  • just noticed you wanted an the Alpha in the HEX so i removed my answer. 99% of the time when i see hex is required it's because transparency is not supported. – Franck Sep 26 '18 at 13:43
  • I have the case now where transparency is not supported but it is required... Thanks for help. – FIL Sep 26 '18 at 13:45