How to

convert Color to hex code

Published: 21. October 2011 | Updated: 26. October 2011
License: Microsoft Public License (MS-PL)
Categories: Converting » Colors
Tags: C# Colors Conversion Winforms WPF
Was this snippet helpful for you? YESYES / NONO

Import namespaces

// in WinForms
using System.Drawing;

// in WPF
using System.Windows.Media;

Code

public static string ToHexColor(Color color, bool alphaChannel)
{
    return String.Format("#{0}{1}{2}{3}",
                         alphaChannel ? color.A.ToString("X2") : String.Empty,
                         color.R.ToString("X2"),
                         color.G.ToString("X2"),
                         color.B.ToString("X2"));
}
Send us feedback about this snippet »



Related Snippets: