Color Picker

Color Picker

The GetPixel function returns the color of a pixel.  This example shows how to use this function by getting the color of the pixel at the current cursor position – a Color Picker function.  It also shows how to examine the image as painted on the screen.  

The example uses a timer.  A each tick of the timer the area of the screen at the mouse pointer position is copied into a bitmap that is created for the purpose.  The graphics object created from the bitmap is then used to copy in the screen at the mouse pointer position, so that the GetPixel method can be used with the bitmap to examine the color of the pixel.  The form background is set to this color.  Changing the form background causes the form’s Paint event to be invoked, and in this event is some code to display the cursor position and the RGB values of the color at that position (that is, the current background color of the form).  The brush color for this text is selected so that it always contrasts with the form’s background color.

To create the example, start a new project named ColorPicker, add a timer to the default form, and paste this code.

Public Class ColorPicker

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  'Set up the time and start it
  Timer1.Interval = 200
  Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  'Set the form's background color to the pixel color
  Me.BackColor = ColorPicker()
End Sub

Function ColorPicker() As Color
  'Create a tiny bitmap
  Dim bmp As New Bitmap(1, 1, Imaging.PixelFormat.Format32bppPArgb)
  'Get a graphics object to work with
  Dim gr As Graphics = Graphics.FromImage(bmp)
  'Copy in the pixel at the cursor position 
  gr.CopyFromScreen(Cursor.Position, Point.Empty, New Size(1, 1))
  'Get and return the pixel color
  Dim PixelColor As Color = bmp.GetPixel(0, 0)
  Return PixelColor
End Function

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
  'Create a local variable for the background color
  Dim BC As Color = Me.BackColor
  'Calculate a brightness value for the background
  Dim S As Integer = CInt(BC.R) + CInt(BC.G) + CInt(BC.B)
  'Create a color object (default black)
  Dim C As Color = Color.Black
  'Change the color according to the brightness
  If S < 384 Then C = Color.White
  'Create a brush of the right color
  Dim myBrush As New SolidBrush(C)
  'Draw the string
  e.Graphics.DrawString(Cursor.Position.ToString & " " & BC.ToString, Me.Font, myBrush, 10.0, 10.0)
  End Sub
End Class
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s