ColorDialog shows the standard Windows color picker and returns a System.Drawing.Color. It is part of Windows Forms, so it only runs on Windows: the project needs net8.0-windows with UseWindowsForms, as created by dotnet new winforms:
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Basic usage
private void ColorButton_Click(object? sender, EventArgs e)
{
using var dialog = new ColorDialog
{
Color = previewPanel.BackColor,
FullOpen = true
};
if (dialog.ShowDialog(this) == DialogResult.OK)
{
previewPanel.BackColor = dialog.Color;
}
}
Setting Color first opens the dialog with the current color selected. FullOpen = true shows the custom color section (the spectrum and the RGB/HSL fields) straight away instead of hiding it behind the “Define Custom Colors” button. If you don’t want users to define their own colors at all, set AllowFullOpen = false.
ShowDialog(this) makes the dialog modal to your form. Only read Color when the result is DialogResult.OK; after Cancel it still holds whatever you preselected.
Keep custom colors between calls
The 16 “Custom colors” slots are lost when the dialog is disposed. To keep them, store the CustomColors array in a field and hand it back next time:
private int[] customColors = [];
private void ColorButton_Click(object? sender, EventArgs e)
{
using var dialog = new ColorDialog
{
Color = previewPanel.BackColor,
FullOpen = true,
CustomColors = customColors
};
if (dialog.ShowDialog(this) == DialogResult.OK)
{
previewPanel.BackColor = dialog.Color;
}
customColors = dialog.CustomColors;
}
Two things about CustomColors trip people up. The getter returns a copy, so changing an element of the returned array does nothing; assign a whole array. And the values are not ARGB integers but Windows COLORREF values in 0x00BBGGRR order. Convert with ColorTranslator:
int colorRef = ColorTranslator.ToOle(Color.SteelBlue);
Color color = ColorTranslator.FromOle(colorRef);
Passing Color.SteelBlue.ToArgb() instead gives you a slot with red and blue swapped.
Comparing colors
Color equality compares more than the RGB values. A named color and the same color built from numbers are not equal:
Color named = Color.Red;
Color fromDialog = Color.FromArgb(255, 0, 0);
Console.WriteLine(named == fromDialog); // False
Console.WriteLine(named.ToArgb() == fromDialog.ToArgb()); // True
Whether a Color carries a name depends on how it was created, and you can’t count on what the dialog returns either way. Compare with ToArgb() when you check what the user picked.
Showing the value as hex
For a settings file or a web color, format the channels yourself:
Color c = dialog.Color;
string hex = $"#{c.R:X2}{c.G:X2}{c.B:X2}";
To read it back, ColorTranslator.FromHtml("#4682B4") parses the same format.
WPF and other UI frameworks
ColorDialog belongs to Windows Forms. WPF has no built-in color picker; WPF apps either reference Windows Forms for this one dialog or use a third-party control. In a WinForms app, the dialog above is all you need.