Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 2.0
System.Drawing
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
ColorConverter Class
Converts colors from one data type to another. Access this class through the TypeDescriptor.

Namespace: System.Drawing
Assembly: System.Drawing (in system.drawing.dll)

Visual Basic (Declaration)
Public Class ColorConverter
    Inherits TypeConverter
Visual Basic (Usage)
Dim instance As ColorConverter
C#
public class ColorConverter : TypeConverter
C++
public ref class ColorConverter : public TypeConverter
J#
public class ColorConverter extends TypeConverter
JScript
public class ColorConverter extends TypeConverter
XAML
Not applicable.

When converting from a string to a Color the ColorConverter expects the unqualified color name; otherwise, an exception will occur in the conversion process. For example, you should pass "Blue", not "System.Drawing.Color.Blue" or "Color.Blue", to the ConvertFrom method.

The following code example demonstrates how to use the ConvertToString method. This example is designed to be used with Windows Forms. Paste this code into a form and call the ShowColorConverter method when handling the form's Paint event, passing e as PaintEventArgs.

Visual Basic
Private Sub ShowColorConverter(ByVal e As PaintEventArgs)

    Dim myColor As Color = Color.PaleVioletRed

    ' Create the ColorConverter.
    Dim converter As System.ComponentModel.TypeConverter = _
       System.ComponentModel.TypeDescriptor.GetConverter(myColor)

    Dim colorAsString As String = _
        converter.ConvertToString(Color.PaleVioletRed)
    e.Graphics.DrawString(colorAsString, Me.Font, _
        Brushes.PaleVioletRed, 50.0F, 50.0F)
End Sub
C#
private void ShowColorConverter(PaintEventArgs e)
{

    Color myColor = Color.PaleVioletRed;

    // Create the ColorConverter.
    System.ComponentModel.TypeConverter converter = 
        System.ComponentModel.TypeDescriptor.GetConverter(myColor);

    string colorAsString = converter.ConvertToString(Color.PaleVioletRed);
    e.Graphics.DrawString(colorAsString, this.Font,
        Brushes.PaleVioletRed, 50.0F, 50.0F);
}
C++
void ShowColorConverter( PaintEventArgs^ e )
{
   Color myColor = Color::PaleVioletRed;
   
   // Create the ColorConverter.
   System::ComponentModel::TypeConverter^ converter = System::ComponentModel::TypeDescriptor::GetConverter( myColor );
   String^ colorAsString = converter->ConvertToString( Color::PaleVioletRed );
   e->Graphics->DrawString( colorAsString, this->Font, Brushes::PaleVioletRed, 50.0F, 50.0F );
}
J#
private void ShowColorConverter(PaintEventArgs e)
{
    Color myColor = Color.get_PaleVioletRed();

    // Create the ColorConverter.
    System.ComponentModel.TypeConverter converter = 
        System.ComponentModel.TypeDescriptor.GetConverter(myColor);
    String colorAsString = converter.ConvertToString(
        Color.get_PaleVioletRed());

    e.get_Graphics().DrawString(colorAsString, this.get_Font(), 
        Brushes.get_PaleVioletRed(), 50, 50);
} //ShowColorConverter
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker