A method to get the pixel color under the mouse pointer

This small code sample shows how to obtain the RGB color of any pixel under the cursor.


The code uses a timer control to fire events ever 100'th of a second and then uses the GetPixel, GetCursorPos, and GetDC WinAPI calls to obtain the value.

option Explicit
'Aaron Young
'Analyst Programmer
'ajyoung@pressenter.com <mailto:ajyoung@pressenter.com>

'Add a Timer Control to a Form, then use this code And point to
'anywhere on your screen to have
'the RGB value appear In the Forms Caption.
private Type POINTAPI
    x as Long
    y as Long
End Type
private Declare Function GetPixel Lib "gdi32" (byval hdc as Long, _
    byval x as Long, byval y as Long) as Long
private Declare Function GetCursorPos Lib "user32" _
        (lpPoint as POINTAPI) as Long
private Declare Function GetWindowDC Lib "user32" (byval hwnd as Long) _
        as Long
private Sub Form_Load()
    Timer1.Interval = 100
End Sub
private Sub Timer1_Timer()
    Dim tPOS as POINTAPI
    Dim sTmp as string
    Dim lColor as Long
    Dim lDC as Long
    lDC = GetWindowDC(0)
    Call GetCursorPos(tPOS)
    lColor = GetPixel(lDC, tPOS.x, tPOS.y)
    Label2.BackColor = lColor
    sTmp = Right$("000000" & Hex(lColor), 6)
    Caption = "R:" & Right$(sTmp, 2) & " G:" &_
         mid$(sTmp, 3, 2) & " B:" & Left$(sTmp, 2)
End Sub

Download zipped project file


  • Pixel Color

    Posted by uruk on 08/12/2005 04:59am

    GetPixel is too slow. Is there any fast way?

  • Hex conversion data is incorrect

    Posted by Legacy on 01/10/2004 12:00am

    Originally posted by: Mark Ross

    The function "lColor = GetPixel(lDC, tPOS.x, tPOS.y)" returns the value 13522332 which is the correct color I pointed at.

    Your Hex conversion routine returns:
    R:9C G:55 B:CE

    When using Microsoft Paint to look at the same pixel, it returns the values:

    R:99 G:55 B:C9

    I can't see what you are doing incorrect, but I can say that the hex values you provide do not recreate the originl pixel color.

  • extract color from images that store in database

    Posted by Legacy on 07/21/2003 12:00am

    Originally posted by: maya

    i am developing an application that store image in database. the images are in single foreground color(eg:star in red color) with single background color(white). what the program should do is to detect/extract specific color choose by user and to match it with images in database.
    eg: user click "red", program display all the images that red in color in the database. please help me.tq.
    -something to do with the GetPixel method.. but i dont know the excat code and how to implement GetPixel?

    • the api functions

      Posted by VBperson on 05/10/2004 10:06pm

      public function GetActiveWindow lib "user32" () as long public function GetCursorPos lib "user32" (lpPoint as PointAPI) as long public function GetDC lib "user32" (byval hwnd as long) as long

    • using getpixel

      Posted by VBperson on 05/10/2004 10:01pm

      I have not tried this myself, but it may work. Use the GetActiveWindow function from the API to get the window that you click on. Then use the GetDC from the API to give you the device context (dc) of the window. then get the cursor position using GetCurosrPos from the API. Pass the dc and the cursor position to the get pixel. Now, that is the theory. if you do not know all of the api functions that i mentioned then right a message saying that you don't and maybe I will read it and tell you.

  • rgb color

    Posted by Legacy on 02/13/2002 12:00am

    Originally posted by: polo

    the lDC = GetWindowDC(0)

    every 100 milisecond

    "ldc" add 4 kilobytes to the memory programm

    it's crazy, after a long time the programm freeze!

    please find an another solution

  • http://codeguru.developer.com/vb/articles/2008.shtml

    Posted by Legacy on 02/15/2000 12:00am

    Originally posted by: SLN

    I just got a project to build a computerized kiosk and was trying some Graphics!! 

    A very good piece of code.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

  • The advent of compute accelerators, special purpose coprocessors that significantly improve the performance of traditional host-based central processing unit (CPU) computations, represents a major transition in the tools available to achieve supercomputing power. This white paper presents Dell solution offerings and shows how they push the envelope of available solutions that bring immediate added value to HPC users in terms of integration, flexibility, and efficiency of hybrid compute solutions.

Most Popular Programming Stories

More for Developers

RSS Feeds