如何:使用复合模式控制 Alpha 混合
有时可能希望创建具有以下特征的离屏位图:
颜色的 alpha 值小于 255。
在创建位图时颜色之间不进行 alpha 混合。
在显示完成的位图时,在显示设备上,位图中的颜色与背景色进行 alpha 混合。
若要创建这样的位图,请构造一个空白的 Bitmap 对象,然后基于该位图构造 Graphics 对象。 将 Graphics 对象的复合模式设置为 CompositingMode.SourceCopy。
示例
下面的示例创建一个基于 Bitmap 对象的 Graphics 对象。 该代码使用 Graphics 对象和两个半透明画笔 (alpha = 160) 在该位图上绘画。 该代码使用半透明画笔填充红色椭圆和绿色椭圆。 绿色椭圆与红色椭圆重叠,但是绿色并不与红色混合,这是因为 Graphics 对象的复合模式已设置为 SourceCopy。
该代码在屏幕上绘制该位图两次:一次是在白色背景上,一次是在多色背景上。 位图中属于两个椭圆的像素的 alpha 分量的值是 160,因此这些椭圆与屏幕上的背景色相混合。
下面的插图显示代码示例的输出。 请注意,这些椭圆与背景相混合,但椭圆之间不进行混合。
代码示例包含这条语句:
bitmapGraphics.CompositingMode = CompositingMode.SourceCopy
bitmapGraphics.CompositingMode = CompositingMode.SourceCopy;
如果您希望这些椭圆除了与背景相混合外,椭圆之间也相互混合,请将上述语句更改为如下语句:
bitmapGraphics.CompositingMode = CompositingMode.SourceOver
bitmapGraphics.CompositingMode = CompositingMode.SourceOver;
下面的插图显示修改后的代码的输出。
' 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)
// 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);
编译代码
前面的示例是为使用 Windows 窗体而设计的,它需要 PaintEventHandler 的参数 PaintEventArgs e。