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.

Screen-Shot

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>
'http://www.pressenter.com/~ajyoung
'

'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


Comments

  • Pixel Color

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

    GetPixel is too slow. Is there any fast way?

    Reply
  • 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.

    Reply
  • 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

      Reply
    • 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.

      Reply
    Reply
  • 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

    Reply
  • 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.
    Thanks!

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

Top White Papers and Webcasts

  • Live Event Date: September 10, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild". This loop of continuous delivery and continuous feedback is …

  • Adaptation and evolution are fundamental requirements of survival -- not only in nature, but also in business. Our world has changed dramatically in a short amount of time. Many businesses are fueling and capitalizing on this change, while others are desperately clinging to a bygone era. Who is left standing in the years and decades ahead should come as no surprise. This edition of Unleashing IT highlights the companies that are embracing new circumstances, new methods, and new opportunities. By downloading …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds