2

I use C# and asp.net 4.

How to convert and Hexadecimal Color in an equivalent RGB Color?

Example: FF0000 to 255,0,0.

My aim is to pass this color to the method FromArgb namespace System.Drawing

FromArgb Method (Int32, Int32, Int32) (Alpha is implicit as opaque)

At the moment I use this code to Clear and Image and apply a opaque flat color:

System.Drawing.Graphics.Clear(System.Drawing.Color.Red));

This works fine with predefined color like "Red", "Black" and so on, but not obviously with a Custom more precise color.

Any ideas? Thanks for your help!

Resource:

http://msdn.microsoft.com/en-us/library/cce5h557.aspx

http://msdn.microsoft.com/en-us/library/system.drawing.color.aspx

GibboK
  • 64,078
  • 128
  • 380
  • 620

2 Answers2

6

Why don't you just do this?

System.Drawing.Color myColor = System.Drawing.Color.FromArgb(0xFF0000);  
Edwin de Koning
  • 13,489
  • 6
  • 52
  • 71
  • How to pass it to the Clear() Method? – GibboK Apr 05 '11 at 14:56
  • @GibboK: System.Drawing.Graphics.Clear(myColor); – Hans Kesting Apr 05 '11 at 14:57
  • Hi I try but does not work. I mean no error and the color it is there but maybe the Clear() method is not my option. What I can use instead? – GibboK Apr 05 '11 at 15:02
  • In this format it is working: System.Drawing.Graphics.Clear(System.Drawing.Color.Red)); if I pass instead the color as you suggest so System.Drawing.Graphics.Clear(myColor) does NOT work. Any Ideas? – GibboK Apr 05 '11 at 15:04
  • @GibboK: What do you mean by 'does not work'? Are you getting an exception or do you not see the color? – Edwin de Koning Apr 05 '11 at 15:56
  • No exceptions just I do not see the color. I also try to debug it and I can see the color in Clear(myColor) but just does not work. What could be? Thanks for your time :-) – GibboK Apr 05 '11 at 16:14
  • @GibboK: What type of graphics object do you call clear on? Do you do so inside a Paint EventHandler? – Edwin de Koning Apr 05 '11 at 16:27
  • I do not use Paint EventHandler. I use Object Image System.Drawing.Bitmap. My code is wrapped in a Class in ASP.NETWebForm. – GibboK Apr 05 '11 at 18:11
0

This answer may help: C# convert integer to hex and back again

Community
  • 1
  • 1
Ed Chapel
  • 6,752
  • 3
  • 25
  • 43