Export (0) Print
Expand All

Color.FromArgb Method

Creates a Color structure from the four 8-bit ARGB component (alpha, red, green, and blue) values.

Overload List

Creates a Color structure from a 32-bit ARGB value.

Supported by the .NET Compact Framework.

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

Creates a Color structure from the specified Color structure, but with the new specified alpha value. Although this method allows a 32-bit value to be passed for the alpha value, the value is limited to 8 bits.

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

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.

Supported by the .NET Compact Framework.

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

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.

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

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 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.
[Visual Basic, C#] Note   This example shows how to use one of the overloaded versions of FromArgb. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Public Sub FromArgb_1(e As PaintEventArgs)
Dim g As Graphics = e.Graphics
' Transparent red, green, and blue brushes.
Dim trnsRedBrush As New SolidBrush(Color.FromArgb(120, 255, 0, 0))
Dim trnsGreenBrush As New SolidBrush(Color.FromArgb(120, 0, _
255, 0))
Dim trnsBlueBrush As New 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 circle.
Dim triBase As Single = 100
Dim triHeight As Single = CSng(Math.Sqrt((3 *(triBase * _
triBase) / 4)))
' Coordinates of first circle's bounding rectangle.
Dim x1 As Single = 40
Dim y1 As Single = 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)
End Sub
        
[C#] 
public void FromArgb_1(PaintEventArgs e)
{
Graphics     g = e.Graphics;
// Transparent red, green, and blue brushes.
SolidBrush trnsRedBrush = new SolidBrush(Color.FromArgb(120, 255, 0, 0));
SolidBrush trnsGreenBrush = new SolidBrush(Color.FromArgb(120, 0, 255, 0));
SolidBrush trnsBlueBrush = new 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);
}
        

[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.

See Also

Color Structure | Color Members | System.Drawing Namespace

Show:
© 2014 Microsoft