Color.ToString Method

Converts this Color structure to a human-readable string.

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

public:
virtual String^ ToString () override
public String ToString ()
public override function ToString () : String
Not applicable.

Return Value

A string that is the name of this Color, if the Color is created from a predefined color by using either the FromName method or the FromKnownColor method; otherwise, a string that consists of the ARGB component names and their values.

A predefined color is also called a known color and is represented by an element of the KnownColor enumeration. When the ToString method is applied to a Color structure that is created by using the FromArgb method, ToString returns a string that consists of the ARGB component names and their values, even if the ARGB value matches the ARGB value of a predefined color.

The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. The code performs the following actions:

  • Iterates through the KnownColor enumeration elements to find all known colors that have a non-zero green component and a zero-value red component and that are not system colors.

  • During each iteration, saves the KnownColor element—if it matches the criteria—in an array.

  • Uses a brush to paint rectangles. Each of the rectangles is painted a KnownColor that matches the criteria stated in the first step. The name of the KnownColor and its component values are also displayed.

This example displays certain known colors and uses ToString to display the names of the colors and their four component values.

void ToArgbToStringExample2( PaintEventArgs^ e )
{
   Graphics^ g = e->Graphics;

   // Color structure used for temporary storage.
   Color someColor = Color::FromArgb( 0 );

   // Array to store KnownColor values that match the criteria.
   array<KnownColor>^colorMatches = gcnew array<KnownColor>(167);

   // Number of matches found.
   int count = 0;

   // Iterate through the KnownColor enums to find all corresponding colors
   // that have a nonzero green component and zero-value red component and
   // that are not system colors.
   for ( KnownColor enumValue = (KnownColor)0; enumValue <= KnownColor::YellowGreen; enumValue = enumValue + (KnownColor)1 )
   {
      someColor = Color::FromKnownColor( enumValue );
      if ( someColor.G != 0 && someColor.R == 0 &&  !someColor.IsSystemColor )
               colorMatches[ count++ ] = enumValue;
   }
   SolidBrush^ myBrush1 = gcnew SolidBrush( someColor );
   System::Drawing::Font^ myFont = gcnew System::Drawing::Font( "Arial",9 );
   int x = 40;
   int y = 40;

   // Iterate through the matches that were found and display each color that
   // corresponds with the enum value in the array. also display the name of
   // the KnownColor and the ARGB components.
   for ( int i = 0; i < count; i++ )
   {
      // Display the color.
      someColor = Color::FromKnownColor( colorMatches[ i ] );
      myBrush1->Color = someColor;
      g->FillRectangle( myBrush1, x, y, 50, 30 );
      
      // Display KnownColor name and the four component values. To display the
      // component values:  Use the ToArgb method to get the 32-bit ARGB value
      // of someColor, which was created from a KnownColor. Then create a
      // Color structure from the 32-bit ARGB value and set someColor equal to
      // this new Color structure. Then use the ToString method to convert it to
      // a string.
      g->DrawString( someColor.ToString(), myFont, Brushes::Black, (float)x + 55, (float)y );
      someColor = Color::FromArgb( someColor.ToArgb() );
      g->DrawString( someColor.ToString(), myFont, Brushes::Black, (float)x + 55, (float)y + 15 );
      y += 40;
   }
}

public void ToArgbToStringExample2(PaintEventArgs e)
{
    Graphics g = e.get_Graphics();

    // Color structure used for temporary storage.
    Color someColor = Color.FromArgb(0);

    // Array to store KnownColor values that match the criteria.
    KnownColor colorMatches[] = new KnownColor[167];

    // Number of matches found.
    int count = 0;

    // Iterate through the KnownColor enums to find all corresponding 
    // colors that have a nonzero green component and zero-value red 
    // component and that are not system colors.
    for (KnownColor enumValue = (KnownColor)0; 
            enumValue.CompareTo(KnownColor.YellowGreen) <= 0; enumValue++) {

        someColor = Color.FromKnownColor(enumValue);
        if (someColor.get_G() != 0 && someColor.get_R() == 0 && 
            !(someColor.get_IsSystemColor())) {
            colorMatches.set_Item(count++, enumValue);
        }
    }

    SolidBrush myBrush1 = new SolidBrush(someColor);
    Font myFont = new Font("Arial", 9);
    int x = 40;
    int y = 40;

    // Iterate through the matches that were found and display each color 
    // that corresponds with the enum value in the array. also display the 
    // name of the KnownColor and the ARGB components.
    for (int i = 0; i < count; i++)    {
        // Display the color.
        someColor = Color.FromKnownColor(
            (KnownColor)colorMatches.get_Item(i));
        myBrush1.set_Color(someColor);
        g.FillRectangle(myBrush1, x, y, 50, 30);

        // Display KnownColor name and the four component values. 
        // To display the component values:  Use the ToArgb method to get 
        // the 32-bit ARGB value of someColor, which was created from a 
        // KnownColor. Then create a Color structure from the 32-bit ARGB 
        // value and set someColor equal to this new Color structure. 
        // Then use the ToString method to convert it to a string.
        g.DrawString(someColor.ToString(), myFont, Brushes.get_Black(), 
         x + 55, y);
        someColor = Color.FromArgb(someColor.ToArgb());
        g.DrawString(someColor.ToString(), myFont, Brushes.get_Black(),
         x + 55, y + 15);
        y += 40;
    }
} //ToArgbToStringExample2

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

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show: