Export (0) Print
Expand All

Color.ToArgb Method

Gets the 32-bit ARGB value of this Color structure.

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

public int ToArgb()

Return Value

Type: System.Int32
The 32-bit ARGB value of this Color.

The byte-ordering of the 32-bit ARGB value is AARRGGBB. The most significant byte (MSB), represented by AA, is the alpha component value. The second, third, and fourth bytes, represented by RR, GG, and BB, respectively, are the color components red, green, and blue, respectively

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, 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 bullet. The name of the KnownColor and its component values are also displayed.

This example displays certain known colors, the names of the colors, and their four component values. The ToArgb method is used as a preliminary step to display the component values.

public void ToArgbToStringExample1(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.
    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 = 0;
        enumValue <= KnownColor.YellowGreen; enumValue++)
    {
        someColor = Color.FromKnownColor(enumValue);
        if (someColor.G != 0 && someColor.R == 0 && !someColor.IsSystemColor)
            colorMatches[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(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, x + 55, y);
        someColor = Color.FromArgb(someColor.ToArgb());
        g.DrawString(someColor.ToString(), myFont, Brushes.Black, x + 55, y + 15);
        y += 40;
    }
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft