The ColorConverter and ColorTranslater Classes in GDI+


This article has been excerpted from book "Graphics Programming with GDI+".

The ColorConverter class is used to convert colors from one data type to another. This class is inherited from the TypeConverter class, which defines the functionality for conversion of type and accessing values and properties of types. The TypeConverter class serves as a base class for many conversion classes, and ColorConverter and FontConverter are two of them. We will discuss FontConverter in more detail later in this article. Some of the common methods of TypeConverter class (which are available in the ColorConverter class) are described in Table5.2.

TABLE 5.2 Common TypeConverter methods

Method

Description

CanConvertForm

Takes a type as a parameter and returns true if the converter can convert an object to the type of the converter; otherwise returns false

CanConverTo

Takes a type as a parameter and returns true if the converter can converts an object to a given type; other wise return false.

ConvertFrom

Converts an object to the type of converter and   returns the converted object

ConvetTo

Converts a specified object to the new   and   returns the object

GetStanderdValues

Returns a collection of standard values (collection type) for the data type for which this type converter is designed.

GetStandardValuesSupported

Identifies whether this object supports a standard set of values.

Listing 5.4 uses the ColorConverter class methods to convert colors. We store in string and call the ConverterFromString method, which returns the Color object. Later we will use the Color objects to create two brushes that we will use to fill a rectangle and an ellipse.

LISTING 5.4 Using the ColorConverter class to convert colors

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = this.CreateGraphics();
            g.Clear(this.BackColor);
            String str = "#FF00FF";
            ColorConverter clrConverter = new ColorConverter();
            Color clr1 = (Color)clrConverter.ConvertFromString(str);

            //Use colors
            SolidBrush clr2 = new SolidBrush(clr1);
            SolidBrush clr3 = new SolidBrush(clr1);

            //Draw GDI+ objects
            g.FillEllipse(clr2, 10, 10, 50, 50);
            g.FillRectangle(clr3, 60, 10, 50, 50);

            //Dispose of objects
            clr2.Dispose();
            clr3.Dispose();
            g.Dispose();
        }
    }
}

fig5.4.gif

FIGURE 5.4: Converting colors

Figure 5.4 shows the output from Listing 5.4.

The ColorTranslator class provides methods to translate colors to and from HTML, OLE, and Winthe-colorconverter-and-colortranslater-classes-in-gdi color values. These methods are useful when you are using legacy color structures that pre-date the .NET Framework. For example, you may have legacy code that gives the HTML color representation of a color. Table 5.3 describes the methods of the ColorTranslator class. All of the methods are static.

Listing 5.5 uses the ColorTranslator class to translate colors from Winthe-colorconverter-and-colortranslater-classes-in-gdi and HTML colors. Later these colors will be used to create brushes.

LISTING 5.5 Translating colors

private void ColorTranslator_click(object sender,System.EventArgs e)
{
    Graphics g =this.CreateGraphics();

     //Translate colors
    Color winthe-colorconverter-and-colortranslater-classes-in-gdicolor = ColorTranslator.Formwinthe-colorconverter-and-colortranslater-classes-in-gdi(oxFF0033);
    Color htmlColor = ColorTranslator.FromHtml("#00AAFF");

     //use Colors
    SolidBrush clr1 =new SolidBrush(winthe-colorconverter-and-colortranslater-classes-in-gdiColor);
    SolidBrush clr2 = new SolidBrush(htmlColor);

     //Draw GDI+
    g.FillEllipse(clr1,10 ,10 ,50 ,50);
    g.FillRectangle(clr2,60 ,10 ,50 ,50);

     //dispose of object
    clr1.Dispose();
    clr2.Dispose();
    g.Dispose();
}

TABLE 5:3 ColorTranslator methods

Method Description

FromHtml

Translates from an HTML color representation to a Color structure

FromOle

Translates from an OLE color value to a Color structure

FromWinthe-colorconverter-and-colortranslater-classes-in-gdi

Translates from an windows color to a Color structure

ToHtml

Translates from a Color structure to HTML color representation.

ToOle

Translates from a Color structure to OLE color.

ToWinthe-colorconverter-and-colortranslater-classes-in-gdi

Translates from a Color structure to window color.

In a manner similar to the "from" methods just discussed, you can translate a Color structure into Winthe-colorconverter-and-colortranslater-classes-in-gdi, HTML and OLE values using the Towinthe-colorconverter-and-colortranslater-classes-in-gdi, ToHtml, and ToOle method, respectively.

Note: You can also transform colors using transformation methods. Some of transformation methods are for scaling, translating, rotating, and shearing. 

Conclusion

Hope the article would have helped you in understanding ColorConverter and ColorTranslater Classes in GDI+. Read other articles on GDI+ on the website.

bookGDI.jpg
This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows.


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.