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: May 18, 2015 While the idea of using facial and/or gesture recognition to create a modern, intuitive game seems attractive, some developers may want to leverage Unity 3D as a way to accelerate their development efforts. There are many different ways in which Intel and Unity Technologies have been working together to help speed the development of games with the Intel&reag; RealSense™ SDK (software developer kit). Check out this webcast to join a panel of experts as they …

  • Companies rely on their telephone and voice systems to maintain services and products to their customers. Yet many experience outages with no plans in place to ensure a speedy recovery. A solid business continuity plan can help maintain uptime and reduce the headaches and fires IT has to deal with when an outage occurs. This white paper takes a deeper look into the common causes of voice failures, how companies deal with voice outages today, and how to determine the right business continuity solution for your …

Most Popular Programming Stories

More for Developers

RSS Feeds

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