Export (0) Print
Expand All

Color.FromArgb Method (Int32, Int32, Int32)

.NET Framework 1.1

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.

[Visual Basic]
Overloads Public Shared Function FromArgb( _
   ByVal red As Integer, _
   ByVal green As Integer, _
   ByVal blue As Integer _
) As Color
[C#]
public static Color FromArgb(
 int red,
 int green,
 int blue
);
[C++]
public: static Color FromArgb(
 int red,
 int green,
 int blue
);
[JScript]
public static function FromArgb(
   red : int,
 green : int,
 blue : int
) : Color;

Parameters

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

Return Value

The Color structure that this method creates.

Example

[Visual Basic, C#] The following 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:

  • Creates Color structures from the three color component values (red, green, blue). Three Color structures are created, one for each primary color.
  • Iterates through a range of alpha values, changing the alpha value of a color.
  • During each iteration: sets the color of a brush to the modified color and paints a rectangle to show the color.
  • Repeats steps 2 and 3 for each primary color.

[Visual Basic, C#] The alpha value is never fully opaque and the rectangles overlap so you get color-combination effects.

[Visual Basic] 
Public Sub FromArgb_2_4(e As PaintEventArgs)
Dim g As Graphics = e.Graphics
' Opaque colors (alpha value defaults to 255 -- max value).
Dim red As Color = Color.FromArgb(255, 0, 0)
Dim green As Color = Color.FromArgb(0, 255, 0)
Dim blue As Color = Color.FromArgb(0, 0, 255)
' Solid brush initialized to red.
Dim myBrush As New SolidBrush(red)
Dim alpha As Integer
Dim x As Integer = 50 ' x coordinate of first red rectangle
Dim y As Integer = 50 ' y coordinate of first red rectangle
' Fill rectangles with red, varying the alpha value from 25 to 250.
For alpha = 25 To 250 Step 25
myBrush.Color = Color.FromArgb(alpha, red)
g.FillRectangle(myBrush, x, y, 50, 100)
g.FillRectangle(myBrush, x, y + 250, 50, 50)
x += 50
Next alpha
x = 50 ' x coordinate of first green rectangle
y += 50 ' y coordinate of first green rectangle
' Fill rectangles with green, varying alpha value from 25 to 250.
For alpha = 25 To 250 Step 25
myBrush.Color = Color.FromArgb(alpha, green)
g.FillRectangle(myBrush, x, y, 50, 150)
x += 50
Next alpha
x = 50 ' x coordinate of first blue rectangle
y += 100 ' y coordinate of first blue rectangle
' Fill rectangles with blue, varying alpha value from 25 to 250.
For alpha = 25 To 250 Step 25
myBrush.Color = Color.FromArgb(alpha, blue)
g.FillRectangle(myBrush, x, y, 50, 150)
x += 50
Next alpha
End Sub
        
[C#] 
public void FromArgb_2_4(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;
int x = 50;         // x coordinate of first red rectangle
int y = 50;         // y coordinate of first red rectangle
// 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 = 50;             // x coordinate of first green rectangle
y += 50;            // y coordinate of first green rectangle
// 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 = 50;             // x coordinate of first blue rectangle
y += 100;           // y coordinate of first blue rectangle
// 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;
}
}
        

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

Color Structure | Color Members | System.Drawing Namespace | Color.FromArgb Overload List

Show:
© 2015 Microsoft