次の方法で共有


方法 : カラー行列を使用してイメージのアルファ値を設定する

Bitmap クラス (Image クラスから継承) および ImageAttributes クラスには、ピクセル値を取得したり設定したりするための機能が用意されています。 ImageAttributes クラスを使用してイメージ全体のアルファ値を変更したり、Bitmap クラスの SetPixel メソッドを呼び出して個々のピクセル値を変更したりできます。

使用例

ImageAttributes クラスには多数のプロパティがあり、これらのプロパティを使用して、描画中のイメージを修正できます。 ImageAttributes オブジェクトを使用して、すべてのアルファ値を元の 80% の値に設定する例を次に示します。 このためには、カラー行列を初期化し、行列内のアルファ スケーリング値を 0.8 に設定します。 カラー行列のアドレスがImageAttributes オブジェクトの SetColorMatrix メソッドに渡され、ImageAttributes オブジェクトが Graphics オブジェクトの DrawString メソッドに渡されます。

描画中に、ビットマップのアルファ値が、元の 80% の値に変換されます。 これにより、描画されるイメージが、背景とブレンドされるようになります。 次の図で示すように、ビットマップ イメージは透明に見えるため、そのイメージを透過して黒の実線を見ることができます。

行列を使用したアルファ ブレンド

イメージが背景の白い部分の上にあるところでは、そのイメージ部分が白色とブレンドされています。 イメージが黒い線を横切るところでは、そのイメージ部分が黒色とブレンドされます。

        ' Create the Bitmap object and load it with the texture image.
        Dim bitmap As New Bitmap("Texture.jpg")

        ' Initialize the color matrix.
        ' Note the value 0.8 in row 4, column 4.
        Dim matrixItems As Single()() = { _
           New Single() {1, 0, 0, 0, 0}, _
           New Single() {0, 1, 0, 0, 0}, _
           New Single() {0, 0, 1, 0, 0}, _
           New Single() {0, 0, 0, 0.8F, 0}, _
           New Single() {0, 0, 0, 0, 1}}

        Dim colorMatrix As New ColorMatrix(matrixItems)

        ' Create an ImageAttributes object and set its color matrix.
        Dim imageAtt As New ImageAttributes()
        imageAtt.SetColorMatrix( _
           colorMatrix, _
           ColorMatrixFlag.Default, _
           ColorAdjustType.Bitmap)

        ' First draw a wide black line.
        e.Graphics.DrawLine( _
           New Pen(Color.Black, 25), _
           New Point(10, 35), _
           New Point(200, 35))

        ' Now draw the semitransparent bitmap image.
        Dim iWidth As Integer = bitmap.Width
        Dim iHeight As Integer = bitmap.Height

        ' Pass in the destination rectangle (2nd argument) and the x _
        ' coordinate (3rd argument), x coordinate (4th argument), width _
        ' (5th argument), and height (6th argument) of the source rectangle.
        e.Graphics.DrawImage( _
           bitmap, _
           New Rectangle(30, 0, iWidth, iHeight), _
           0.0F, _
           0.0F, _
           iWidth, _
           iHeight, _
           GraphicsUnit.Pixel, _
           imageAtt)

// Create the Bitmap object and load it with the texture image.
Bitmap bitmap = new Bitmap("Texture.jpg");

// Initialize the color matrix.
// Note the value 0.8 in row 4, column 4.
float[][] matrixItems ={ 
   new float[] {1, 0, 0, 0, 0},
   new float[] {0, 1, 0, 0, 0},
   new float[] {0, 0, 1, 0, 0},
   new float[] {0, 0, 0, 0.8f, 0}, 
   new float[] {0, 0, 0, 0, 1}};
ColorMatrix colorMatrix = new ColorMatrix(matrixItems);

// Create an ImageAttributes object and set its color matrix.
ImageAttributes imageAtt = new ImageAttributes();
imageAtt.SetColorMatrix(
   colorMatrix,
   ColorMatrixFlag.Default,
   ColorAdjustType.Bitmap);

// First draw a wide black line.
e.Graphics.DrawLine(
   new Pen(Color.Black, 25),
   new Point(10, 35),
   new Point(200, 35));

// Now draw the semitransparent bitmap image.
int iWidth = bitmap.Width;
int iHeight = bitmap.Height;
e.Graphics.DrawImage(
   bitmap,
   new Rectangle(30, 0, iWidth, iHeight),  // destination rectangle
   0.0f,                          // source rectangle x 
   0.0f,                          // source rectangle y
   iWidth,                        // source rectangle width
   iHeight,                       // source rectangle height
   GraphicsUnit.Pixel,
   imageAtt);

コードのコンパイル

前述の例は Windows フォームと一緒に使用することが想定されていて、PaintEventHandler のパラメーターである PaintEventArgs e が必要です。

参照

その他の技術情報

Windows フォームにおけるグラフィックスと描画

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