How to convert System.Drawing.Color code to RGB or Hexa?

Recently i came across an need of converting System.Drawing.Color value to equivalent RGB value and display it on the UI.

In fact there is no inbuilt function in .Net to do this. But we can do this with little effort. Let's see how can we do this.

Code snippet:
string ColorCodeToRGBString(Color col)
{
    return String.Format("{0},{1},{2}", col.R.ToString(), col.G.ToString(), col.B.ToString());


string ColorCodeToHexaDecimalString(Color col)
{
    return String.Format("#{0},{1},{2}", col.R.ToString("X2"), col.G.ToString("X2"), col.B.ToString("X2"));
}

That's it.




If you feel this is helpful or you like it, Please share this using share buttons available on page.

Comments

Popular posts from this blog

Routed Events in WPF

Get Laptop battery status using C#

(C#) How to parse float numbers with IFormatProvider?