How to Create a Colour Chooser/Grabber in Visual Basic

Introduction: Welcome to a tutorial on how to create a colour chooser in Visual Basic. Steps of Creation: Step 1: First we want to create a form. Mine has three textboxes (r,g,b) to show to red, green and blue colours of the current colour and a Panel to contain the colour image. Step 2: Now we want to begin the timer on form load so it is instant. You can make a start/stop button if you want.
  1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  2.     Timer1.Enabled = True
  3.     Timer1.Interval = 100
  4.     Timer1.Start()
  5. End Sub
Step 3: Now we want to make the timer tick function. We simply create a new bitmap and get the colours from the mouse position on screen. We then send that to the panel1 and set the textboxes.
  1. Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
  2.     Dim BMP As New Drawing.Bitmap(1, 1)
  3.     Dim GFX As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(BMP)
  4.     GFX.CopyFromScreen(New Drawing.Point(MousePosition.X, MousePosition.Y), New Drawing.Point(0, 0), BMP.Size)
  5.     Dim Pixel As Drawing.Color = BMP.GetPixel(0, 0)
  6.     Panel1.BackColor = Pixel
  7.     r.Text = Pixel.R
  8.     g.Text = Pixel.G
  9.     b.Text = Pixel.B
  10. End Sub
Project Complete! That's it! Below is the full source code and download to the project files.
  1. Public Class CC
  2.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  3.         Timer1.Enabled = True
  4.         Timer1.Interval = 100
  5.         Timer1.Start()
  6.     End Sub
  7.  
  8.     Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
  9.         Dim BMP As New Drawing.Bitmap(1, 1)
  10.         Dim GFX As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(BMP)
  11.         GFX.CopyFromScreen(New Drawing.Point(MousePosition.X, MousePosition.Y), New Drawing.Point(0, 0), BMP.Size)
  12.         Dim Pixel As Drawing.Color = BMP.GetPixel(0, 0)
  13.         Panel1.BackColor = Pixel
  14.         r.Text = Pixel.R
  15.         g.Text = Pixel.G
  16.         b.Text = Pixel.B
  17.     End Sub
  18. End Class

Add new comment