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.
Comments
Post a Comment