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

Auto Scroll in Common Controls

Convert typed library (.tlb) to .net assembly?

Disable close button on form - C#