Graphics.GetNearestColor(Color) 方法

定义

获取与指定的 Color 结构最接近的颜色。Gets the nearest color to the specified Color structure.

public:
 System::Drawing::Color GetNearestColor(System::Drawing::Color color);
public System.Drawing.Color GetNearestColor (System.Drawing.Color color);
member this.GetNearestColor : System.Drawing.Color -> System.Drawing.Color
Public Function GetNearestColor (color As Color) As Color

参数

color
Color

Color 结构,为其查找匹配项。Color structure for which to find a match.

返回

Color

一个 Color 结构,它表示与 color 参数指定的颜色最接近的颜色。A Color structure that represents the nearest color to the one specified with the color parameter.

示例

下面的代码示例旨在与 Windows 窗体一起使用,并且它需要作为 PaintEventArgs e Paint 事件处理程序的参数。The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. 此代码执行以下操作:The code performs the following actions:

  • 使用 ARGB 坐标 (255、165、63、136) 创建任意颜色。Creates an arbitrary color with ARGB coordinates (255, 165, 63, 136).

  • 创建纯色画笔,并将其颜色设置为指定的颜色。Creates a solid brush and sets its color to the specified color.

  • 使用任意颜色填充椭圆。Fills an ellipse using the arbitrary color.

  • 创建第二种颜色,并将其值设置为最接近的系统 ARGB 颜色。Creates a second color and sets its value to the nearest system ARGB color.

  • 使用此颜色填充第二个椭圆。Fills a second ellipse with this color.

结果就是两个省略号:第一次用任意指定的颜色绘制,第二个绘制时的系统颜色与指定的颜色最接近。The result is two ellipses: the first drawn with the arbitrary specified color and the second drawn with the system color nearest the specified color.

public:
   void GetNearestColorColor( PaintEventArgs^ e )
   {
      // Create solid brush with arbitrary color.
      Color arbColor = Color::FromArgb( 255, 165, 63, 136 );
      SolidBrush^ arbBrush = gcnew SolidBrush( arbColor );

      // Fill ellipse on screen.
      e->Graphics->FillEllipse( arbBrush, 0, 0, 200, 100 );

      // Get nearest color.
      Color realColor = e->Graphics->GetNearestColor( arbColor );
      SolidBrush^ realBrush = gcnew SolidBrush( realColor );

      // Fill ellipse on screen.
      e->Graphics->FillEllipse( realBrush, 0, 100, 200, 100 );
   }
private void GetNearestColorColor(PaintEventArgs e)
{
    // Create solid brush with arbitrary color.
    Color arbColor = Color.FromArgb(255, 165, 63, 136);
    SolidBrush arbBrush = new SolidBrush(arbColor);

    // Fill ellipse on screen.
    e.Graphics.FillEllipse(arbBrush, 0, 0, 200, 100);

    // Get nearest color.
    Color realColor = e.Graphics.GetNearestColor(arbColor);
    SolidBrush realBrush = new SolidBrush(realColor);

    // Fill ellipse on screen.
    e.Graphics.FillEllipse(realBrush, 0, 100, 200, 100);
}
Private Sub GetNearestColorColor(ByVal e As PaintEventArgs)

    ' Create solid brush with arbitrary color.
    Dim arbColor As Color = Color.FromArgb(255, 165, 63, 136)
    Dim arbBrush As New SolidBrush(arbColor)

    ' Fill ellipse on screen.
    e.Graphics.FillEllipse(arbBrush, 0, 0, 200, 100)

    ' Get nearest color.
    Dim realColor As Color = e.Graphics.GetNearestColor(arbColor)
    Dim realBrush As New SolidBrush(realColor)

    ' Fill ellipse on screen.
    e.Graphics.FillEllipse(realBrush, 0, 100, 200, 100)
End Sub

适用于