Blog of a Filipino Developer about C#, VB.NET, ASP.NET, Java, PHP, SQL Server, MySql and Oracle RSS 2.0
# Friday, September 05, 2008

Ever heard of ColorTranslator? It's a really cool class that you can use to convert colors in .NET. One of the things that it can do is that enables you to convert HTML colors to Windows colors and vice versa. One particular scenario wherein you might want to use this class is when you want to use HTML colors on your WinForm.

Below are examples on how to use this calls:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace ColorTranslatorTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //convert a system.drawing.color to html
            Color c1 = Color.AntiqueWhite;
            button1.BackColor = c1;
            button1.Text = ColorTranslator.ToHtml(c1);

            //convert an html color to a gdi+ color
            string htmlColor = "#FFAD55";
            Color c2 = ColorTranslator.FromHtml(htmlColor);
            button2.BackColor = c2;
            button2.Text = ColorTranslator.ToHtml(c2);

            //convert a win32 color to gdi+ color
            int colorHexadecimalValue = 0xA267;
            Color c3 = ColorTranslator.FromWin32(colorHexadecimalValue);
            button3.BackColor = c3;
            button3.Text = ColorTranslator.ToWin32(c3).ToString("X");
        }
    }
}

HTH

Friday, September 05, 2008 5:59:10 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] -
.NET | Tutorial
Archive
<September 2008>
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Keith Rull
Sign In
Statistics
Total Posts: 271
This Year: 0
This Month: 0
This Week: 0
Comments: 182
Themes
Pick a theme:
All Content © 2010, Keith Rull
DasBlog theme 'Business' created by Christoph De Baene (delarou)