Export (0) Print
Expand All

Color.FromArgb Method (Int32, Int32, Int32)

Creates a Color structure from the specified 8-bit color values (red, green, and blue). The alpha value is implicitly 255 (fully opaque). Although this method allows a 32-bit value to be passed for each color component, the value of each component is limited to 8 bits.

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

public static Color FromArgb(
	int red,
	int green,
	int blue
)

Parameters

red
Type: System.Int32

The red component value for the new Color. Valid values are 0 through 255.

green
Type: System.Int32

The green component value for the new Color. Valid values are 0 through 255.

blue
Type: System.Int32

The blue component value for the new Color. Valid values are 0 through 255.

Return Value

Type: System.Drawing.Color
The Color that this method creates.

ExceptionCondition
ArgumentException

red, green, or blue is less than 0 or greater than 255.

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:

  1. Creates Color structures from the three color component values (red, green, blue). Three Color structures are created, one for each primary color.

  2. Iterates through a range of alpha values, changing the alpha value of a color.

  3. During each iteration, sets the color of a brush to the modified color and paints a rectangle to show the color.

  4. Repeats steps 2 and 3 for each primary color.

The alpha value is never fully opaque and the rectangles overlap so you get color-combination effects.

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

    // Opaque colors (alpha value defaults to 255 -- max value).
    Color red = Color.FromArgb(255, 0, 0);
    Color green = Color.FromArgb(0, 255, 0);
    Color blue = Color.FromArgb(0, 0, 255);

    // Solid brush initialized to red.
    SolidBrush  myBrush = new SolidBrush(red);
    int alpha;

    // x coordinate of first red rectangle 
    int x = 50;         

    // y coordinate of first red rectangle 
    int y = 50;         

    // Fill rectangles with red, varying the alpha value from 25 to 250. 
    for (alpha = 25; alpha <= 250; alpha += 25)
    {
        myBrush.Color = Color.FromArgb(alpha, red);
        g.FillRectangle(myBrush, x, y, 50, 100);
        g.FillRectangle(myBrush, x, y + 250, 50, 50);
        x += 50;
    }
    // x coordinate of first green rectangle.
    x = 50;             

    // y coordinate of first green rectangle.
    y += 50;            

    // Fill rectangles with green, varying the alpha value from 25 to 250. 
    for (alpha = 25; alpha <= 250; alpha += 25)
    {
        myBrush.Color = Color.FromArgb(alpha, green);
        g.FillRectangle(myBrush, x, y, 50, 150);
        x += 50;
    }
    // x coordinate of first blue rectangle.
    x = 50;             

    // y coordinate of first blue rectangle.
    y += 100;           

    // Fill rectangles with blue, varying the alpha value from 25 to 250. 
    for (alpha = 25; alpha <= 250; alpha += 25)
    {
        myBrush.Color = Color.FromArgb(alpha, blue);
        g.FillRectangle(myBrush, x, y, 50, 150);
        x += 50;
    }
}

.NET Framework

Supported in: 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