C# WinForms: The Color Selector
C# WinForms: The Color Selector
Introduction
Microsoft Windows provides a function used to get a color stored in a pixel. The .NET Framework supports this operation in the Bitmap class, which is equipped with a method named GetPixel. By calling this method, you can retrieve the color of a pixel.
Practical Learning: Creating the Application


| Control | Text | Name | Other Properties | |
| PictureBox | PbxColorSelector | Image: ColorPalette.png | ||
| Label | Number | |||
| Label | HEX | |||
| Label | Red: | |||
| TextBox | TxtNumberRed | |||
| TextBox | TxtHexadecimalRed | |||
| Label | Green: | |||
| TextBox | TxtNumberGreen | |||
| TextBox | TxtHexadecimalGreen | |||
| Label | Blue: | |||
| TextBox | TxtNumberBlue | |||
| TextBox | TxtHexadecimalBlue | |||
| Panel |
|
pnlPreview | BorderStyle: FixedSingle | |
| Label | Color: | |||
| TextBox | TxtHexadecimalColor | |||
| Button | Close | BtnClose | ||
Public Class Exercise
Dim IsSelecting As Boolean = False
Private Sub PbxColorSelector_MouseDown(sender As Object, e As MouseEventArgs) Handles PbxColorSelector.MouseDown
IsSelecting = True
End Sub
Private Sub PbxColorSelector_MouseUp(sender As Object, e As MouseEventArgs) Handles PbxColorSelector.MouseUp
IsSelecting = False
End Sub
Private Sub PbxColorSelector_MouseMove(sender As Object, e As MouseEventArgs) Handles PbxColorSelector.MouseMove
If IsSelecting Then
Dim Bitmap As Bitmap = CType(PbxColorSelector.Image, Bitmap)
Dim Color As Color = Bitmap.GetPixel(e.X, e.Y)
TxtHexadecimalRed.Text = Color.R.ToString("X2")
TxtHexadecimalGreen.Text = Color.G.ToString("X2")
TxtHexadecimalBlue.Text = Color.B.ToString("X2")
TxtNumberRed.Text = Color.R.ToString()
TxtNumberGreen.Text = Color.G.ToString()
TxtNumberBlue.Text = Color.B.ToString()
TxtColor.Text = $"#{Color.R:X2}{Color.G:X2}{Color.B:X2}"
PnlPreview.BackColor = Color
End If
End Sub
Private Sub BtnClose_Click(sender As Object, e As EventArgs) Handles BtnClose.Click
Close()
End Sub
End Class


|
|
|||
| Home | Copyright © 2010-2026, FunctionX | Wednesday 25 March 2026, 16:34 | Home |
|
|
|||