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.

ApplicationPractical Learning: Creating the Application

  1. Copy the following picture and paste it somewhere in your computer

    Color Palette

  2. Start Microsoft Visual Studio
  3. On the Visual Studio dialog box, click Create New Project
  4. In the Create a New Project dialog box, click Windows Forms App
  5. Click Next
  6. Set the Name to ColorSelector
  7. Click Next
  8. In the Framework combo box, select the highest version and click Create
  9. In the Solution Explorer, right-click Form1.cs and click Rename
  10. Type Exercise (to get Exercise.cs) and press Enter twice
  11. Design the form as follows:

    Color Selector

    Control Text Name Other Properties
    PictureBox Picture Box   PbxColorSelector Image: ColorPalette.png
    Label Label Number    
    Label Label HEX    
    Label Label Red:    
    TextBox TextBox   TxtNumberRed  
    TextBox TextBox   TxtHexadecimalRed  
    Label Label Green:    
    TextBox TextBox   TxtNumberGreen  
    TextBox TextBox   TxtHexadecimalGreen  
    Label Label Blue:    
    TextBox TextBox   TxtNumberBlue  
    TextBox TextBox   TxtHexadecimalBlue  
    Panel Panel   pnlPreview BorderStyle: FixedSingle
    Label Label Color:    
    TextBox TextBox   TxtHexadecimalColor  
    Button Button Close BtnClose  
  12. Right-click the form and click View Code
  13. Change the document as follows:
    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
  14. To execute the application, on the main menu, click Debug and click Start Without Debugging:

  15. Click and hold the mouse on the picture boxmouse and drag to produce a color:

  16. Close the form

Color Selector


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