如何:使用组合模式控制 Alpha 混合

有时可能需要创建具有以下特征的屏幕外位图:

  • 颜色具有小于 255 的 alpha 值。

  • 创建位图时,颜色不会相互混合。

  • 显示完成的位图时,位图中的颜色与显示设备上的背景色混合。

若要创建此类位图,请构造一个空白 Bitmap 对象,然后基于该位图构造对象 Graphics 。 将对象的组合模式 Graphics 设置为 CompositingMode.SourceCopy

示例:

以下示例基于Bitmap对象创建一个Graphics对象。 该代码使用 Graphics 对象以及两个半透明画笔(alpha = 160)在位图上绘制。 代码使用半透明画笔填充红色椭圆和绿色椭圆。 绿色椭圆与红色椭圆重叠,但绿色不与红色混合,因为对象的合成模式 Graphics 设置为 SourceCopy

该代码在屏幕上绘制位图两次:一次位于白色背景上,一次位于多色背景上。 位图中属于两个椭圆的像素具有 160 的 alpha 分量,因此椭圆与屏幕上的背景色混合。

下图显示了代码示例的输出。 请注意,椭圆与背景混合,但它们不会相互混合。

展示椭圆形与背景融合而非彼此融合的图示。

代码示例包含以下语句:

bitmapGraphics.CompositingMode = CompositingMode.SourceCopy;
bitmapGraphics.CompositingMode = CompositingMode.SourceCopy

如果要将省略号与彼此以及背景混合,请将该语句更改为以下内容:

bitmapGraphics.CompositingMode = CompositingMode.SourceOver;
bitmapGraphics.CompositingMode = CompositingMode.SourceOver

下图显示了修改后的代码的输出。

显示与背景混合的省略号的关系图。

// Create a blank bitmap.
Bitmap myBitmap = new Bitmap(180, 100);

// Create a Graphics object that we can use to draw on the bitmap.
Graphics bitmapGraphics = Graphics.FromImage(myBitmap);

// Create a red brush and a green brush, each with an alpha value of 160.
SolidBrush redBrush = new SolidBrush(Color.FromArgb(160, 255, 0, 0));
SolidBrush greenBrush = new SolidBrush(Color.FromArgb(160, 0, 255, 0));

// Set the compositing mode so that when we draw overlapping ellipses,
// the colors of the ellipses are not blended.
bitmapGraphics.CompositingMode = CompositingMode.SourceCopy;

// Fill an ellipse using a red brush that has an alpha value of 160.
bitmapGraphics.FillEllipse(redBrush, 0, 0, 150, 70);

// Fill a second ellipse using a green brush that has an alpha value of 160.
// The green ellipse overlaps the red ellipse, but the green is not
// blended with the red.
bitmapGraphics.FillEllipse(greenBrush, 30, 30, 150, 70);

// Set the compositing quality of the form's Graphics object.
e.Graphics.CompositingQuality = CompositingQuality.GammaCorrected;

// Draw a multicolored background.
SolidBrush colorBrush = new SolidBrush(Color.Aqua);
e.Graphics.FillRectangle(colorBrush, 200, 0, 60, 100);
colorBrush.Color = Color.Yellow;
e.Graphics.FillRectangle(colorBrush, 260, 0, 60, 100);
colorBrush.Color = Color.Fuchsia;
e.Graphics.FillRectangle(colorBrush, 320, 0, 60, 100);

// Display the bitmap on a white background.
e.Graphics.DrawImage(myBitmap, 0, 0);

// Display the bitmap on a multicolored background.
e.Graphics.DrawImage(myBitmap, 200, 0);
' Create a blank bitmap.
Dim myBitmap As New Bitmap(180, 100)

' Create a Graphics object that we can use to draw on the bitmap.
Dim bitmapGraphics As Graphics = Graphics.FromImage(myBitmap)

' Create a red brush and a green brush, each with an alpha value of 160.
Dim redBrush As New SolidBrush(Color.FromArgb(160, 255, 0, 0))
Dim greenBrush As New SolidBrush(Color.FromArgb(160, 0, 255, 0))

' Set the compositing mode so that when we draw overlapping ellipses,
' the colors of the ellipses are not blended.
bitmapGraphics.CompositingMode = CompositingMode.SourceCopy

' Fill an ellipse using a red brush that has an alpha value of 160.
bitmapGraphics.FillEllipse(redBrush, 0, 0, 150, 70)

' Fill a second ellipse using a green brush that has an alpha value of 
' 160. The green ellipse overlaps the red ellipse, but the green is not 
' blended with the red.
bitmapGraphics.FillEllipse(greenBrush, 30, 30, 150, 70)

'Set the compositing quality of the form's Graphics object. 
e.Graphics.CompositingQuality = CompositingQuality.GammaCorrected

' Draw a multicolored background.
Dim colorBrush As New SolidBrush(Color.Aqua)
e.Graphics.FillRectangle(colorBrush, 200, 0, 60, 100)
colorBrush.Color = Color.Yellow
e.Graphics.FillRectangle(colorBrush, 260, 0, 60, 100)
colorBrush.Color = Color.Fuchsia
e.Graphics.FillRectangle(colorBrush, 320, 0, 60, 100)

'Display the bitmap on a white background.
e.Graphics.DrawImage(myBitmap, 0, 0)

' Display the bitmap on a multicolored background.
e.Graphics.DrawImage(myBitmap, 200, 0)

编译代码

前面的示例专用于 Windows 窗体,它需要 PaintEventArgse,这是 PaintEventHandler 的参数。

另请参阅