Share via


方法 : 透過性でイメージを描画します。

[このドキュメントはプレビュー版であり、後のリリースで変更されることがあります。 空白のトピックは、プレースホルダーとして挿入されています。]

.NET Compact Framework サポート、透明度が 1 つだけの透過色。 SetColorKey(Color, Color) メソッド High Color 範囲ロー カラーを指定した同じ色があります。

使用例

この例は、赤と黒の設計と四角形の Bitmap を作成し、透明度を設定するための 2 つの手法を示します。

  • イメージ内のピクセルに基づいて、SetColorKey(Color, Color) メソッド使用します。 次の使用例の透明度、イメージの範囲の左上隅にあるピクセルを使用して設定します。 このピクセルは黒であるため、すべての最初の黒のピクセルは透明なります。

  • 明示的な色の設定には、SetColorKey(Color, Color) メソッドを使用します。 次の使用例設定赤、すべての最初の赤いピクセルは透明にできるようにされます。

デモンストレーションをまず両方透過の手法でアプリケーションを実行なし透過セットで、イメージの表示方法を使用してアウトします。 透明度手法のいずれかのコードが次します。

                        ' The .NET Compact Framework supports transparency,
                        ' but with only one transparency color.
                        ' The SetColorKey method must have the same color 
                        ' specified for the low color and high color range.
                        
' Note that you must uncomment lines of code
                        ' as indicated for the desired transparency effect.
                        Protected
                        Overrides
                        Sub OnPaint(ByVal e As PaintEventArgs)

' Create a red and black bitmap to demonstrate transparency.Dim bmp AsNew Bitmap(75, 75)
    Dim g As Graphics = Graphics.FromImage(bmp)

    g.FillEllipse(New SolidBrush(Color.Red), 0, 0, bmp.Width, bmp.Width)
    g.DrawLine(New Pen(Color.Black), 0, 0, bmp.Width, bmp.Width)
    g.DrawLine(New Pen(Color.Black), bmp.Width, 0, 0, bmp.Width)
    g.Dispose()


Dim attr AsNew ImageAttributes

' Set the transparency color key based on the upper-left pixel ' of the image.' Uncomment the following line to make all black pixels transparent:' attr.SetColorKey(bmp.GetPixel(0, 0), bmp.GetPixel(0, 0))
' Set the transparency color key based on a specified value.' Uncomment the following line to make all red pixels transparent:' attr.SetColorKey(Color.Red, Color.Red)
' Draw the image using the image attributes.Dim dstRect AsNew Rectangle(0, 0, bmp.Width, bmp.Height)
e.Graphics.DrawImage(bmp, dstRect, 0, 0, bmp.Width, bmp.Height, _
    GraphicsUnit.Pixel, attr)

EndSub
                        // The .NET Compact Framework supports transparency,
                        // but with only one transparency color.
                        // The SetColorKey method must have the same color 
                        // specified for the low color and high color range.
                        // Note that you must uncomment lines of code
                        // as indicated for the desired transparency effect.
                        protected
                        override
                        void OnPaint(PaintEventArgs e)
{
    // Create a red and black bitmap to demonstrate transparency.
    Bitmap bmp = new Bitmap(75,75);
    Graphics g = Graphics.FromImage(bmp);

    g.FillEllipse(new SolidBrush(Color.Red), 0, 0, bmp.Width, bmp.Width);
    g.DrawLine(new Pen(Color.Black), 0, 0, bmp.Width, bmp.Width);
    g.DrawLine(new Pen(Color.Black), bmp.Width, 0, 0, bmp.Width);
    g.Dispose();

    ImageAttributes attr = new ImageAttributes();

    // Set the transparency color key based on the upper-left pixel // of the image.// Uncomment the following line to make all black pixels transparent:// attr.SetColorKey(bmp.GetPixel(0, 0), bmp.GetPixel(0, 0));// Set the transparency color key based on a specified value.// Uncomment the following line to make all red pixels transparent:// attr.SetColorKey(Color.Red, Color.Red);// Draw the image using the image attributes.
    Rectangle dstRect = new Rectangle(0, 0, bmp.Width, bmp.Height);
    e.Graphics.DrawImage(bmp, dstRect, 0, 0, bmp.Width, bmp.Height,
        GraphicsUnit.Pixel, attr);
}

コードのコンパイル方法

この例では、次の名前空間への参照が必要です。

堅牢性の高いプログラム

ビットマップの作成に使用される Graphics オブジェクトが明示的に破棄することを確認します。 Graphics オブジェクトの Graphics プロパティによって返される PaintEventArgs オブジェクトはガベージ コレクターによって破棄されする明示的に破棄する必要はありません。

参照

その他の技術情報

グラフィックスと、.NET での描画の最適化フレームワーク