Converting System.Drawing.Color to HTML color (and vice-versa)

#

This is something I need very rarely. So rare in fact that in-between uses it is completely removed from my memory, leaving just the fact that I know I’ve searched for it before!

To convert a .NET Color object to an HTML RGB or named color, use

string colorAsString = ColorTranslator.ToHtml( _backgroundColor ) ;

and vice-versa

Color c = ColorTranslator.FromHtml( @"#RRGGBB" ) ;

🙏🙏🙏

Since you've made it this far, sharing this article on your favorite social media network would be highly appreciated 💖! For feedback, please 🦋 ping me on Bluesky! 🦋

Leave a comment

Comments are moderated, so there may be a short delays before you see it.

Published