HOW TO:使用複合模式控制 Alpha 混色
更新:2007 年 11 月
有時候,您可能會想要建立具有下列特性的螢幕外點陣圖:
色彩中的 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 Form 搭配使用而設計的,而且它需要 PaintEventArgse (即 PaintEventHandler 的參數)。