次の方法で共有


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

次の特性を持つ画面外ビットマップを作成したい場合があります。

  • 色のアルファ値は 255 未満です。

  • ビットマップを作成するとき、色同士は互いにアルファブレンドされません。

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

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

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

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

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

互いにではなく背景とブレンドされた楕円を示す図。

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

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

こちらも参照ください