共用方式為


如何:使用不透明和半透明筆刷繪製

當您填滿形狀時,您必須傳遞 Brush 物件至 Graphics 類別的其中一種填滿方法。 SolidBrush 建構函式的參數是 Color 物件。 若要填滿不透明的圖形,請設定色彩的 Alpha 元件為 255。 若要填滿半透明的圖案,請設定 Alpha 元件為從 1 到 254 的任何值。

當您填滿半透明的圖案時,圖案的色彩會與背景的色彩混合。 Alpha 元件指定如何混合圖案和背景色彩;接近於 0 的 Alpha 值給予背景色彩較多的權重,而接近 255 的 Alpha 值給予圖形色彩較多的權重。

範例

下列範例會繪製點陣圖,然後填滿重疊點陣圖的三個橢圓形。 第一個橢圓形使用的 Alpha 元件為 255,所以是不透明的。 第二個和第三個橢圓形使用的 Alpha 元件為 128,因此是半透明的;您可以看到穿透橢圓形的背景影像。 設定 CompositingQuality 屬性的呼叫會讓第三個橢圓形搭配 Gamma 修正進行混色。

Bitmap bitmap = new Bitmap("Texture1.jpg");
e.Graphics.DrawImage(bitmap, 50, 50, bitmap.Width, bitmap.Height);

SolidBrush opaqueBrush = new SolidBrush(Color.FromArgb(255, 0, 0, 255));
SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(128, 0, 0, 255));

e.Graphics.FillEllipse(opaqueBrush, 35, 45, 45, 30);
e.Graphics.FillEllipse(semiTransBrush, 86, 45, 45, 30);

e.Graphics.CompositingQuality = CompositingQuality.GammaCorrected;
e.Graphics.FillEllipse(semiTransBrush, 40, 90, 86, 30);
Dim bitmap As New Bitmap("Texture1.jpg")
e.Graphics.DrawImage(bitmap, 50, 50, bitmap.Width, bitmap.Height)

Dim opaqueBrush As New SolidBrush(Color.FromArgb(255, 0, 0, 255))
Dim semiTransBrush As New SolidBrush(Color.FromArgb(128, 0, 0, 255))

e.Graphics.FillEllipse(opaqueBrush, 35, 45, 45, 30)
e.Graphics.FillEllipse(semiTransBrush, 86, 45, 45, 30)

e.Graphics.CompositingQuality = CompositingQuality.GammaCorrected
e.Graphics.FillEllipse(semiTransBrush, 40, 90, 86, 30)

下圖顯示下列程式碼的輸出:

Illustration that shows opaque and semitransparent output.

編譯程式碼

上述範例設計是為搭配 Windows Form 使用所設計,而且需要 PaintEventArgse,這是 PaintEventHandler 的參數。

另請參閱