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

  • Companies undertaking an IT project need to find the right balance between cost and functionality. It's important to start by determining whether to build a solution from scratch, buy an out-of-the-box solution, or a combination of both. In reality, most projects will require some system tailoring to meet business requirements. Decision-makers must understand how much software development is enough and craft a detailed implementation plan to ensure the project's success. This white paper examines the different …

  • On-demand Event Event Date: February 12, 2015 The evolution of systems engineering with the SysML modeling language has resulted in improved requirements specification, better architectural definition, and better hand-off to downstream engineering. Agile methods have proven successful in the software domain, but how can these methods be applied to systems engineering? Check out this webcast and join Bruce Powel Douglass, author of Real-Time Agility, as he discusses how agile methods have had a tremendous …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date