次の方法で共有


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

更新 : 2007 年 11 月

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

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

  • ビットマップを作成しても、色が相互にアルファ ブレンドされることはない。

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

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

使用例

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

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

コード例による出力を次の図に示します。2 つの楕円は、背景とはブレンドされますが、相互にブレンドされることはありません。

ソース コピー

このコード例には、次のようなステートメントがあります。

bitmapGraphics.CompositingMode = CompositingMode.SourceCopy

bitmapGraphics.CompositingMode = CompositingMode.SourceCopy;

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

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 のパラメータである PaintEventArgse が必要です。

参照

参照

FromArgb

その他の技術情報

アルファ ブレンドの直線と塗りつぶし