Getting pixel colors from another application's window

Posted on 2003-11-07
Last Modified: 2010-05-01

I need to be able to get the colors of pixels in another application's window. Currently i use the GetPixel API in my vb app and this works quite well. The problem is that GetPixel fails if the window your getting values from is minimised or covered etc. Does anybody know how i can get pixel color values from another window even if it is minimised? i have been searching for ages for a better way than Getpixel but no luck yet.

Question by:Redux

Expert Comment

ID: 9701942

As you found out, GetPixel will only get the color of an on-screen pixel.  If an app is minimzed or hidden, it's not on-screen!   The application is responsible for repainting the window when it is restored or maximized, so there really isn't a pixel color on the window when it is not visible.

Don't know if this helps, but you could probably use the API GetDC() to get the device context for the window (I'm assuming you have the window's handle), then use GetBkColor() to get the background color for that context.  This will return the default background color for the device context, but if the app draws on top of the background, the pixel color is not guaranteed.  

I hate to ask why you need to get the color of a pixel on a hidden window...!


Author Comment

ID: 9702314
Thanks for your reply. Yes, I have the handle of the window, and i have been getting the DC using the GetWindowDC API. Unfortunately your suggestion of GetBkColor wont work, because I need to read quite a few pixel colors at different coordinates not just the background color.

As for why I need to do this it is because my program is designed to operate in the background whilst the user is doing other things (hence both my app and the app im getting pixel colors from will be minimised).
LVL 17

Expert Comment

ID: 9704820
The device context is destroyed when not on-screen so getting it won't help either if it's minimized. A sloppy choice for you would be to set the window position to "on top", get the DC, copy it into a compatible bitmap/dc in your application then set the window back to it's original state. The showing/restoring should be only a flicker.
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.


Author Comment

ID: 9706802
    The problem is I am getting calling GetPixel roughly 3 times a second as the pixel colors are changing every so often. So it might not be very pratical to constantly keep pulling the window to the top. Thanks for reply tho. Im increasing the points to 300 as it seems this is a very difficult question. Can anyone else think of a way to read individual pixels on minimised windows? If not I will award the points to zzzzzooc as he seems to have offered a possible solution that would work.

Expert Comment

ID: 9707633
Get the snap shot of the window before its minimized and then scan the pixels of the snap shot.


Author Comment

ID: 9707684
I'm sorry. I don't think i've explained this very well. The program im reading pixels from is displaying numbers on its window. My program uses getpixel to do a basic character recognition to find out what the numbers are and this works fine until the window is minimised or the areas im getpixelling from are covered. The numbers are also changing quite often which means i have to getpixel quite frequently. Obviously this means that i have to be able to getpixel (or some similar method which can retrieve pixel colors) lots of times, not just once. I notice that getpixel wont work on areas of windows that are not in the current clipping area (like obscured parts of the form, as windows just doesnt draw them). Could I maybe alter the clipping region so getpixel works on areas of the window hidden?
If what i'm trying to do isn't possible (get values of pixel colors not currently displayed) then please tell me.
LVL 17

Accepted Solution

zzzzzooc earned 300 total points
ID: 9708869
Only other alternative i've encountered is with WM_PAINT (have never attempted it myself yet):

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now