CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Grant Killian's Blog

No, this has nothing to do with beer -- but maybe it should?

ColorTranslator

I use the ColorTranslator class all the time.  It comes in really handy with web UI development.  I'm always having to match a hexidecimal color (usually courtesy of PhotoShop or ImageReady) and the basic System.Drawing.Color class won't cut it.  Color.AliceBlue and Color.Salmon are fine for demos and playing around, but the real world requires precise color matching!

The syntax for using the ColorTranslator is like this:

pnlMyPanel.BackColor = ColorTranslator.FromHtml( "#CBCB48" ); //c#

ColorTranslator has more methods, so you can convert a windows constant color using FromOle() like this:

me.BackColor = ColorTranslator.FromOle( CType( &HC3B333&, Integer ) ) 'vb.Net -- note the integer conversion!

Happy .Netting!



Comments

Rich said:

Great article. Thanks for explaining about ColorTranslator.FromHtml <i>AND</i> ColorTranslator.FromOle

Just thought I would add that you can use ColorTranslator.FromHtml in VB.Net if you pass in the Hex Numbers as strings (same as you do in C#).
# December 16, 2004 5:13 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!