Login   /   Register

Get pixel color

Rate this article
     3 votes, average: 3.33 out of 53 votes, average: 3.33 out of 53 votes, average: 3.33 out of 53 votes, average: 3.33 out of 53 votes, average: 3.33 out of 5
Loading ... Loading ...
May 26th, 2008 by Yaron Assa

In spite of all the advanced properties and methods of QTP, it’s the simple things which often seem impossible.

A good example would be checking the color of a certain element in our application. Usually, we leave these tests to the manual QA team, stating that QTP isn’t meant for graphic verification (As its bitmap checkpoint mechanism is shaky, at best). This QTip will present a more delicate mechanism than a bitmap check - getting the color of a given pixel in the screen.

In order to do this, we shall relay on several Win32 API function calls. You can learn more about using Win32API in the relevant Scripting QTP’s chapter.

 

First, we declare the relevant functions: GetDC and ReleaseDC for handling graphical contexts, and GetPixel which will return the pixel color code.

Next, we’ll hook up to the desktop main’s display (HWnd = 0), and get its graphic context.

And then we can simply extract the pixel color code, and release the graphical context.

Put together, the entire code looks like this:

Dim hDC, hWnd
Dim iX, iY
 
'Declare win32 and gdi32 functions
Extern.Declare MicLong, "GetPixel", "gdi32″, "",MicLong, MicLong, MicLong
Extern.Declare MicLong, "GetDC", "user32″,"", MicLong
Extern.Declare micLong,"ReleaseDC","user32″,"",micLong,micLong
 
'Attach to the desktop graphical context
hWnd = cLng(0)
hDC = Extern.GetDC(hWnd)
 
iX = 10 'Change this to your relevant X coordinate
iY = 10 'Change this to your relevant Y coordinate
 
'Get the pixel color code
Print Extern.GetPixel (hDC, iX,iY)
 
'Release the graphical context
Extern.ReleaseDC hWnd, hDC 

And the end result will be a printout of the color code (of course you can store it in a variable and use it to perform whatever validation you see fit).

image

Posted in Win32

7 Responses to “Get pixel color”

  1. priya1581 Says:

    [-]

    Really …a useful one.

  2. Yaron Assa Says:

    [-]

    You can thank for avraham mager raising the question

  3. vigor_vigor Says:

    [-]

    Is there any way I could change the screen resolution and color setting at run time?

    Thanks,

  4. pmgrossman Says:

    [+]

    I found this article very useful. I used it in a custom highlighting function to determine if the highlight had been erased by a s... ...

  5. Yaron Assa Says:

    [-]

    Excellent Idea, it’s a feature I never thought possible to validate with QTP.

  6. maxsch Says:

    [-]

    It is great!

  7. Nandini Says:

    [-]

    we are using .net application.We have to find the row highlighting(red color) in Ultra grid.Please could you suggest

Leave a Reply

You must be logged in to post a comment.

This article was viewed 1084 times