Share via


方法: 複合モードを使用してアルファ ブレンドを制御する

状況によっては、次の特性を持つオフスクリーン ビットマップを作成することが必要になる場合があります。

  • 色のアルファ値が 255 未満。

  • ビットマップを作成する際、色がアルファ ブレンドされない。

  • 完成したビットマップを表示すると、ビットマップ内の色が、ディスプレイ デバイスの背景色とアルファ ブレンドされる。

このようなビットマップを作成するには、空の Bitmap オブジェクトを構築した後、そのビットマップに基づく Graphics オブジェクトを構築します。 Graphics オブジェクトの合成モードは CompositingMode.SourceCopy に設定します。

次の例では、Bitmap オブジェクトに基づいて Graphics オブジェクトを作成しています。 このコードでは、Graphics オブジェクトと 2 つの半透明ブラシ (アルファ = 160) を使用して、ビットマップ上の色を描画しています。 このコードを実行すると、赤の楕円と緑の楕円が半透明のブラシで塗りつぶされます。 緑の楕円は赤い楕円と重なっていますが、Graphics オブジェクトの合成モードが SourceCopy に設定されているので、緑が赤とブレンドされることはなりません。

このコードでは、画面上にビットマップが 2 回描画されます。1 回は白の背景に対して、もう 1 回はマルチカラーの背景に対してです。 2 つの楕円の一部になっているビットマップのピクセルについては、アルファ成分が 160 であるため、楕円が画面の背景色とブレンドされます。

次の図は、このコード例の出力を示したものです。 楕円が背景とブレンドされていますが、互いにブレンドされてはいないことに注意してください。

Diagram showing ellipses blended with the background, not each other.

このコード例には、次のステートメントが含まれています。

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

楕円を背景とブレンドするだけでなく、相互にもブレンドしたい場合は、ステートメントを次のように変更します。

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

次の図は、変更後のコードの出力を示したものです。

Diagram that shows ellipses blended together and with background.

// 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 フォームで使用するために設計されていて、PaintEventHandler のパラメーターである PaintEventArgse を必要とします。

関連項目