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

  • Today's competitive marketplace requires the organization to frequently release and deploy applications at the pace of user demands, with reduced cost, risk, and increased quality. This book defines the basics of application release and deployment, and provides best practices for implementation with resources for a deeper dive. Inside you will find: The business and technical drivers behind automated application release and deployment. Evaluation guides for application release and deployment solutions. …

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds