Color.FromArgb 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
从四个 8 位 ARGB 组件(alpha、红色、绿色和蓝色)值创建 Color 结构。
重载
FromArgb(Int32, Int32, Int32, Int32) |
从四个 ARGB 组件(alpha、红色、绿色和蓝色)值创建 Color 结构。 尽管此方法允许为每个组件传递 32 位值,但每个组件的值限制为 8 位。 |
FromArgb(Int32, Int32, Int32) |
根据指定的 8 位颜色值(红色、绿色和蓝色)创建 Color 结构。 alpha 值隐式为 255(完全不透明)。 尽管此方法允许为每个颜色分量传递 32 位值,但每个组件的值限制为 8 位。 |
FromArgb(Int32, Color) |
从指定的 Color 结构创建 Color 结构,但具有新的指定 alpha 值。 尽管此方法允许为 alpha 值传递 32 位值,但该值限制为 8 位。 |
FromArgb(Int32) |
从 32 位 ARGB 值创建 Color 结构。 |
FromArgb(Int32, Int32, Int32, Int32)
- Source:
- Color.cs
- Source:
- Color.cs
- Source:
- Color.cs
从四个 ARGB 组件(alpha、红色、绿色和蓝色)值创建 Color 结构。 尽管此方法允许为每个组件传递 32 位值,但每个组件的值限制为 8 位。
public:
static System::Drawing::Color FromArgb(int alpha, int red, int green, int blue);
public static System.Drawing.Color FromArgb (int alpha, int red, int green, int blue);
static member FromArgb : int * int * int * int -> System.Drawing.Color
Public Shared Function FromArgb (alpha As Integer, red As Integer, green As Integer, blue As Integer) As Color
参数
- alpha
- Int32
alpha 分量。 有效值为 0 到 255。
- red
- Int32
红色组件。 有效值为 0 到 255。
- green
- Int32
绿色组件。 有效值为 0 到 255。
- blue
- Int32
蓝色组件。 有效值为 0 到 255。
返回
此方法创建的 Color。
例外
alpha
、red
、green
或 blue
小于 0 或大于 255。
示例
下面的代码示例设计用于 Windows 窗体,它需要 PaintEventArgse
,这是 Paint 事件处理程序的参数。 该代码执行以下操作:
创建三个画笔,每个画笔的颜色不同。 用于创建画笔的每个 Color 结构都是从四个分量值(alpha、红色、绿色、蓝色)创建的。
使用虚构三角形来定位三个圆。
绘制三个重叠圆,每个圆以三角形的一个顶点为中心,为每个圆使用不同的画笔。
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 );
}
public void FromArgb1(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);
}
Public Sub FromArgb1(ByVal 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
注解
若要创建不透明颜色,请将 alpha
设置为 255。 若要创建半透明颜色,请将 alpha
设置为从 1 到 254 的任何值。
适用于
FromArgb(Int32, Int32, Int32)
- Source:
- Color.cs
- Source:
- Color.cs
- Source:
- Color.cs
根据指定的 8 位颜色值(红色、绿色和蓝色)创建 Color 结构。 alpha 值隐式为 255(完全不透明)。 尽管此方法允许为每个颜色分量传递 32 位值,但每个组件的值限制为 8 位。
public:
static System::Drawing::Color FromArgb(int red, int green, int blue);
public static System.Drawing.Color FromArgb (int red, int green, int blue);
static member FromArgb : int * int * int -> System.Drawing.Color
Public Shared Function FromArgb (red As Integer, green As Integer, blue As Integer) As Color
参数
返回
此方法创建的 Color。
例外
red
、green
或 blue
小于 0 或大于 255。
示例
下面的代码示例设计用于 Windows 窗体,它需要 PaintEventArgse
,这是 Paint 事件处理程序的参数。 该代码执行以下操作:
循环访问一系列 alpha 值,更改颜色的 alpha 值。
每次迭代期间,将画笔的颜色设置为修改的颜色,并绘制一个矩形以显示颜色。
对每个主要颜色重复步骤 2 和步骤 3。
alpha 值永远不会完全不透明,并且矩形重叠,因此可以获得颜色组合效果。
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 = gcnew 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 = 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 = 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;
}
}
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;
}
}
Public Sub FromArgb2(ByVal 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
' x coordinate of first red rectangle.
Dim x As Integer = 50
' y coordinate of first red rectangle.
Dim y As Integer = 50
' 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 coordinate of first green rectangle.
x = 50
' y coordinate of first green rectangle.
y += 50
' 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 coordinate of first blue rectangle.
x = 50
' y coordinate of first blue rectangle.
y += 100
' 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
适用于
FromArgb(Int32, Color)
- Source:
- Color.cs
- Source:
- Color.cs
- Source:
- Color.cs
public:
static System::Drawing::Color FromArgb(int alpha, System::Drawing::Color baseColor);
public static System.Drawing.Color FromArgb (int alpha, System.Drawing.Color baseColor);
static member FromArgb : int * System.Drawing.Color -> System.Drawing.Color
Public Shared Function FromArgb (alpha As Integer, baseColor As Color) As Color
参数
返回
此方法创建的 Color。
例外
alpha
小于 0 或大于 255。
示例
下面的代码示例设计用于 Windows 窗体,它需要 PaintEventArgse
,这是 Paint 事件处理程序的参数。 该代码执行以下操作:
循环访问一系列 alpha 值,更改颜色的 alpha 值。
每次迭代期间,将画笔的颜色设置为修改的颜色,并绘制一个矩形以显示颜色。
对每个主要颜色重复步骤 2 和步骤 3。
alpha 值永远不会完全不透明,并且矩形重叠,因此可以获得颜色组合效果。
void FromArgb3( 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 = gcnew 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 = 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 = 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;
}
}
public void FromArgb3(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;
}
}
Public Sub FromArgb3(ByVal 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
' x coordinate of first red rectangle.
Dim x As Integer = 50
' y coordinate of first red rectangle.
Dim y As Integer = 50
' 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 coordinate of first green rectangle.
x = 50
' y coordinate of first green rectangle.
y += 50
' 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 coordinate of first blue rectangle.
x = 50
' y coordinate of first blue rectangle.
y += 100
' 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
注解
若要创建不透明颜色,请将 alpha
设置为 255。 若要创建半透明颜色,请将 alpha
设置为从 1 到 254 的任何值。
适用于
FromArgb(Int32)
- Source:
- Color.cs
- Source:
- Color.cs
- Source:
- Color.cs
从 32 位 ARGB 值创建 Color 结构。
public:
static System::Drawing::Color FromArgb(int argb);
public static System.Drawing.Color FromArgb (int argb);
static member FromArgb : int -> System.Drawing.Color
Public Shared Function FromArgb (argb As Integer) As Color
参数
- argb
- Int32
指定 32 位 ARGB 值的值。
返回
此方法创建的 Color 结构。
示例
下面的代码示例设计用于 Windows 窗体,它需要 PaintEventArgse
,这是 Paint 事件处理程序的参数。 该代码执行以下操作:
创建三个画笔,每个画笔的颜色不同。 用于创建画笔的每个 Color 结构都是从 32 位 ARGB 值创建的。
使用虚构三角形来定位三个圆。
绘制三个重叠圆,每个圆以三角形的一个顶点为中心,为每个圆使用不同的画笔。
void FromArgb4( PaintEventArgs^ e )
{
Graphics^ g = e->Graphics;
// Transparent red, green, and blue brushes.
SolidBrush^ trnsRedBrush = gcnew SolidBrush( Color::FromArgb( 0x78FF0000 ) );
SolidBrush^ trnsGreenBrush = gcnew SolidBrush( Color::FromArgb( 0x7800FF00 ) );
SolidBrush^ trnsBlueBrush = gcnew SolidBrush( Color::FromArgb( 0x780000FF ) );
// 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 );
}
public void FromArgb4(PaintEventArgs e)
{
Graphics g = e.Graphics;
// Transparent red, green, and blue brushes.
SolidBrush trnsRedBrush = new SolidBrush(Color.FromArgb(0x78FF0000));
SolidBrush trnsGreenBrush = new SolidBrush(Color.FromArgb(0x7800FF00));
SolidBrush trnsBlueBrush = new SolidBrush(Color.FromArgb(0x780000FF));
// 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);
}
Public Sub FromArgb4(ByVal e As PaintEventArgs)
Dim g As Graphics = e.Graphics
' Transparent red, green, and blue brushes.
Dim trnsRedBrush As New SolidBrush(Color.FromArgb(&H78FF0000))
Dim trnsGreenBrush As New SolidBrush(Color.FromArgb(&H7800FF00))
Dim trnsBlueBrush As New SolidBrush(Color.FromArgb(&H780000FF))
' 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
注解
32 位 ARGB 值的字节排序为 AARRGGBB。 AA 表示的最重要字节(MSB)是 alpha 分量值。 第二个、第三个和第四个字节(分别由 RR、GG 和 BB 表示)是颜色分量红色、绿色和蓝色。