Convert color to hex using C#

So you want convert a .NET color to hex or a hex color to .NET color. Well, its very easy to do this. We won’t have to write lots of code or a special class for it. Thanks to ColorTranslator class. We can convert between these with only 1 line of code. Here’s how its done:

To convert a Hex color to .NET color use this code:

Color color = ColorTranslator.FromHtml("#000000");

For converting a .NET color to Hex color, use thise code:

Color color = Color.FromArgb(0, 100, 50, 100);

// Get the hex color
string HexColor = ColorTranslator.ToHtml(color);
 

That was simple & easy. If you have any questions don’t hesitate to leave a comment.

Thanks

4 thoughts on “Convert color to hex using C#

  1. hi,

    First of all. Thanks very much for your useful post.

    I just came across your blog and wanted to drop you a note telling you how impressed I was with the information you have posted here.

    simi

  2. Highly impressed, found your post on Ask.Glad I finally tested it out. Unsure if its my Chrome browser,but sometimes when I visit your site, the fonts are really small? Anyway, love your site and will check back.See Ya

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>