Export (0) Print
Expand All

Graphics.GetNearestColor Method

Gets the nearest color to the specified Color structure.

[Visual Basic]
Public Function GetNearestColor( _
   ByVal color As Color _
) As Color
[C#]
public Color GetNearestColor(
 Color color
);
[C++]
public: Color GetNearestColor(
 Color color
);
[JScript]
public function GetNearestColor(
   color : Color
) : Color;

Parameters

color
Color structure for which to find a match.

Return Value

The method returns a Color structure that represents the nearest color to the one specified with the color parameter.

Example

[Visual Basic, C#] The following example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • 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.
  • Creates a second color and sets its value to the nearest system ARGB color.
  • Fills a second ellipse with this color.

[Visual Basic, C#] 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.

[Visual Basic] 
Public Sub GetNearestColorColor(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
        
[C#] 
public 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);
}
        

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

Graphics Class | Graphics Members | System.Drawing Namespace

Show:
© 2014 Microsoft