次の方法で共有


ImageAttributes.SetColorKey メソッド (Color, Color, ColorAdjustType)

指定したカテゴリのカラー キー (透明度の範囲) を設定します。

Overloads Public Sub SetColorKey( _
   ByVal colorLow As Color, _   ByVal colorHigh As Color, _   ByVal type As ColorAdjustType _)
[C#]
public void SetColorKey(ColorcolorLow,ColorcolorHigh,ColorAdjustTypetype);
[C++]
public: void SetColorKey(ColorcolorLow,ColorcolorHigh,ColorAdjustTypetype);
[JScript]
public function SetColorKey(
   colorLow : Color,colorHigh : Color,type : ColorAdjustType);

パラメータ

  • colorLow
    下位のカラー キー値。
  • colorHigh
    上位のカラー キー値。
  • type
    カラー キーを設定する対象のカテゴリを指定する ColorAdjustType 列挙体の要素。

戻り値

このメソッドは値を返しません。

解説

このメソッドは、透明にする色の範囲を指定する上位と下位のカラー キー値を設定します。色の 3 つのコンポーネント (赤、緑、青) が、それぞれ対応するコンポーネントの上位と下位のカラー キーの間にある場合、その色は透明になります。

ImageAttributes オブジェクトは、5 つの調整カテゴリ (既定、ビットマップ、ブラシ、ペン、テキスト) に関して色とグレースケールの設定を保持します。たとえば、既定のカテゴリに対してあるカラー キーを指定し、ビットマップ カテゴリには別のカラー キーを指定し、さらにペン カテゴリにも異なるカラー キーを指定できます。

既定のカラー調整設定値およびグレースケール調整設定値は、調整設定値が設定されていないすべてのカテゴリに対して適用されます。たとえば、ペン カテゴリの調整設定を一切指定していない場合、ペン カテゴリには既定の設定が適用されます。

特定のカテゴリに対してカラー調整設定値またはグレースケール調整設定値を指定すると、そのカテゴリに適用されていた既定の調整設定値は解除されます。たとえば、既定のカテゴリに対して調整設定値のコレクションを指定するとします。ColorAdjustType.Pen を SetColorKey メソッドに渡してペン カテゴリのカラー キーを設定すると、ペンには既定の調整設定は一切適用されません。

使用例

[Visual Basic, C#] 次の例は、Windows フォームでの使用を意図してデザインされており、 Paint イベント ハンドラのパラメータである PaintEventArgs e が必要です。このコードは次のアクションを実行します。

  • ファイル Circle.bmp を使用する Image オブジェクトを開き、それを画面に描画します。
  • ImageAttributes オブジェクトを作成し、 SetColorKey メソッドを呼び出すことにより、そのオブジェクトのカラー キーを設定します。
  • ImageAttributes オブジェクトのカラー キーを使用して、イメージを画面に描画します。
 
Public Sub SetColorKeyExample(e As PaintEventArgs)
' Open an Image file, and draw it to the screen.
Dim myImage As Image = Image.FromFile("Circle.bmp")
e.Graphics.DrawImage(myImage, 20, 20)
' Create an ImageAttributes object and set the color key.
Dim lowerColor As Color = Color.FromArgb(245, 0, 0)
Dim upperColor As Color = Color.FromArgb(255, 0, 0)
Dim imageAttr As New ImageAttributes()
imageAttr.SetColorKey(lowerColor, upperColor, _
ColorAdjustType.Default)
' Draw the image with the color key set.
Dim rect As New Rectangle(150, 20, 100, 100)
e.Graphics.DrawImage(myImage, rect, 0, 0, 100, 100, _
GraphicsUnit.Pixel, imageAttr) ' Image
End Sub
        
[C#] 
private void SetColorKeyExample(PaintEventArgs e)
{
// Open an Image file and draw it to the screen.
Image myImage = Image.FromFile("Circle.bmp");
e.Graphics.DrawImage(myImage, 20, 20);
// Create an ImageAttributes object and set the color key.
Color lowerColor = Color.FromArgb(245,0,0);
Color upperColor = Color.FromArgb(255,0,0);
ImageAttributes imageAttr = new ImageAttributes();
imageAttr.SetColorKey(lowerColor,
upperColor,
ColorAdjustType.Default);
// Draw the image with the color key set.
Rectangle rect = new Rectangle(150, 20, 100, 100);
e.Graphics.DrawImage(myImage,         // Image
rect,            // Dest. rect.
0,               // srcX
0,               // srcY
100,             // srcWidth
100,             // srcHeight
GraphicsUnit.Pixel, // srcUnit
imageAttr);      // ImageAttributes
}
        

[C++, JScript] C++ および JScript のサンプルはありません。Visual Basic および C# のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

ImageAttributes クラス | ImageAttributes メンバ | System.Drawing.Imaging 名前空間 | ImageAttributes.SetColorKey オーバーロードの一覧