ImageAttributes.SetColorKey 方法

定义

设置颜色键(透明范围)。

重载

SetColorKey(Color, Color, ColorAdjustType)

为指定类别设置颜色键(透明范围)。

SetColorKey(Color, Color)

为默认类别设置颜色键。

SetColorKey(Color, Color, ColorAdjustType)

Source:
ImageAttributes.cs
Source:
ImageAttributes.cs
Source:
ImageAttributes.cs

为指定类别设置颜色键(透明范围)。

public:
 void SetColorKey(System::Drawing::Color colorLow, System::Drawing::Color colorHigh, System::Drawing::Imaging::ColorAdjustType type);
public void SetColorKey (System.Drawing.Color colorLow, System.Drawing.Color colorHigh, System.Drawing.Imaging.ColorAdjustType type);
member this.SetColorKey : System.Drawing.Color * System.Drawing.Color * System.Drawing.Imaging.ColorAdjustType -> unit
Public Sub SetColorKey (colorLow As Color, colorHigh As Color, type As ColorAdjustType)

参数

colorLow
Color

低颜色键值。

colorHigh
Color

高颜色键值。

type
ColorAdjustType

ColorAdjustType 的一个元素,指定将设置颜色键的类别。

示例

下面的代码示例旨在与 Windows 窗体 一起使用,它需要 PaintEventArgse,它是 事件处理程序的Paint一个参数。 此代码执行以下操作:

  1. 打开使用 Image 文件 Circle.bmp 并将其绘制到屏幕的 。

  2. 创建对象 ImageAttributes 并通过调用 SetColorKey 方法设置其颜色键。

  3. 使用 对象的颜色键 ImageAttributes 将图像绘制到屏幕。

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 = gcnew ImageAttributes;
      imageAttr->SetColorKey( lowerColor, upperColor, ColorAdjustType::Default );

      // Draw the image with the color key set.
      Rectangle rect = Rectangle(150,20,100,100);
      e->Graphics->DrawImage( myImage, rect, 0, 0, 100, 100, GraphicsUnit::Pixel, imageAttr );
   }
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, rect, 0, 0, 100, 100, 
        GraphicsUnit.Pixel, imageAttr);      
}
Public Sub SetColorKeyExample(ByVal 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

注解

此方法设置高颜色键值和低颜色键值,以便一系列颜色可以设为透明。 在高颜色键和低颜色键的相应分量之间具有其三个分量 (红、绿、蓝) 的任何颜色都变得透明。

对象维护五个 ImageAttributes 调整类别的颜色和灰度设置:默认值、位图、画笔、笔和文本。 例如,可以为默认类别指定颜色键,为位图类别指定不同的颜色键,并为笔类别指定不同的颜色键。

默认颜色调整和灰度调整设置适用于没有自己调整设置的所有类别。 例如,如果从未为笔类别指定任何调整设置,默认设置将应用于笔类别。

为特定类别指定颜色调整或灰度调整设置后,默认调整设置将不再应用于该类别。 例如,假设为默认类别指定调整设置的集合。 如果通过传递给 PenSetColorKey 方法来设置笔类别的颜色键,则任何默认调整设置都不会应用于笔。

适用于

SetColorKey(Color, Color)

Source:
ImageAttributes.cs
Source:
ImageAttributes.cs
Source:
ImageAttributes.cs

为默认类别设置颜色键。

public:
 void SetColorKey(System::Drawing::Color colorLow, System::Drawing::Color colorHigh);
public void SetColorKey (System.Drawing.Color colorLow, System.Drawing.Color colorHigh);
member this.SetColorKey : System.Drawing.Color * System.Drawing.Color -> unit
Public Sub SetColorKey (colorLow As Color, colorHigh As Color)

参数

colorLow
Color

低颜色键值。

colorHigh
Color

高颜色键值。

示例

有关代码示例,请参阅 SetColorKey(Color, Color, ColorAdjustType) 方法。

注解

此方法设置高颜色键值和低颜色键值,以便一系列颜色可以设为透明。 在高颜色键和低颜色键的相应分量之间具有其三个分量 (红、绿、蓝) 的任何颜色都变得透明。

对象维护五个 ImageAttributes 调整类别的颜色和灰度设置:默认值、位图、画笔、笔和文本。 例如,可以为默认类别指定颜色键,为位图类别指定不同的颜色键,并为笔类别指定不同的颜色键。

默认颜色调整和灰度调整设置适用于没有自己调整设置的所有类别。 例如,如果从未为笔类别指定任何调整设置,默认设置将应用于笔类别。

适用于