public GetNearestColor ( Color color ) : Color | ||
color | Color | |
return | Color |
using System.Drawing; namespace ConsoleApp1 { class Program { static void Main(string[] args) { Graphics graphics = Graphics.FromHwnd(IntPtr.Zero); Color color = Color.FromArgb(120, 50, 200); // Original color Color nearestColor = graphics.GetNearestColor(color); // Nearest color } } }
using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Paint(object sender, PaintEventArgs e) { Graphics graphics = e.Graphics; Color red = Color.Red; Color nearestColor = graphics.GetNearestColor(red); Pen pen = new Pen(nearestColor); Rectangle rect = new Rectangle(20, 20, 100, 100); graphics.DrawRectangle(pen, rect); } } }In this example, we use the same GetNearestColor method to get the nearest color representation of the color red and use it to draw a rectangle on the form using the Graphics.DrawRectangle method. The System.Drawing.Graphics class is part of the .NET Framework library.