Color::FromArgb Method (Int32, Int32, Int32, Int32)

 

Creates a Color structure from the four ARGB component (alpha, red, green, and blue) values. Although this method allows a 32-bit value to be passed for each 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 alpha,
	int red,
	int green,
	int blue
)

Parameters

alpha
Type: System::Int32

The alpha component. Valid values are 0 through 255.

red
Type: System::Int32

The red component. Valid values are 0 through 255.

green
Type: System::Int32

The green component. Valid values are 0 through 255.

blue
Type: System::Int32

The blue component. Valid values are 0 through 255.

Return Value

Type: System.Drawing::Color

The Color that this method creates.

Exception Condition
ArgumentException

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

To create an opaque color, set alpha to 255. To create a semitransparent color, set alpha to any value from 1 through 254.

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:

  • Creates three brushes, each a different color. Each Color structure that is used to create a brush is created from four component values (alpha, red, green, blue).

  • Uses an imaginary triangle to position three circles.

  • Paints three overlapping circles, each centered on one vertex of the triangle, using a different brush for each circle.

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

   // Transparent red, green, and blue brushes.
   SolidBrush^ trnsRedBrush = gcnew SolidBrush( Color::FromArgb( 120, 255, 0, 0 ) );
   SolidBrush^ trnsGreenBrush = gcnew SolidBrush( Color::FromArgb( 120, 0, 255, 0 ) );
   SolidBrush^ trnsBlueBrush = gcnew SolidBrush( Color::FromArgb( 120, 0, 0, 255 ) );

   // Base and height of the triangle that is used to position the
   // circles. Each vertex of the triangle is at the center of one of the
   // 3 circles. The base is equal to the diameter of the circles.
   float triBase = 100;
   float triHeight = (float)Math::Sqrt( 3 * (triBase * triBase) / 4 );

   // Coordinates of first circle's bounding rectangle.
   float x1 = 40;
   float y1 = 40;

   // Fill 3 over-lapping circles. Each circle is a different color.
   g->FillEllipse( trnsRedBrush, x1, y1, 2 * triHeight, 2 * triHeight );
   g->FillEllipse( trnsGreenBrush, x1 + triBase / 2, y1 + triHeight, 2 * triHeight, 2 * triHeight );
   g->FillEllipse( trnsBlueBrush, x1 + triBase, y1, 2 * triHeight, 2 * triHeight );
}

.NET Framework
Available since 1.1
Return to top
Show: