KryTonX

GetPixel - Pixel Color of background Window

6 posts in this topic

#1 ·  Posted (edited)

Hello there,

as I used this function in a lot of projects, I might as well share it :)

It gets a color value from a background (or foreground of course) window by using a combination of WinAPI.au3 and GDIPlus.au3 (might as well be replaced with https://msdn.microsoft.com/en-us/library/windows/desktop/dd144909(v=vs.85).aspx which should work too and is maybe a bit faster, as it wouldn't need to create an extra GDIPlus Bitmap).

 

Long story short; here is the function:

 

;===================================================================================================================================
;
; Description:      Gets the Color Value from a Pixel (possible in background windows [not hidden!])
; Syntax:           GetColor()
; Parameter(s):
;                   $iX - X Coordinate (In the Window, not whole Screen), get it by using the Autoit Window Info tool!
;                   $iY - Y Coordinate (In the Window, not whole Screen), get it by using the Autoit Window Info tool!
;                   $WinHandle - Handle obtained by WinGetHandle
;                   $iWidth - Width of the Window to Capture
;                   $iHeight - Height of the Window to Capture
;
; Return Value(s):  Hex of Color Value
;
; Requirements: 
; #RequireAdmin Braucht Adminrechte falls ihr auf ein Programm mit Adminrechten zugreifen wollt!
;#include <WinAPI.au3>
;#include <WindowsConstants.au3>
;#include <GDIPlus.au3>
;#include <ScreenCapture.au3>
; #include <Array.au3>
;
; Note: Does NOT work on hidden or minimized windows as Windows stops rendering them.
;       If you do not want to put in the $WinHandle all the time, replace the $WinHandle Parameter
;       with $WinHandle = $hwnd (if $hwnd is your Handle obtained by WinGetHandle)
;
;       Tested only on Windows 10 Professional 64 Bit
;
;===================================================================================================================================

Func GetColor($iX,$iY,$WinHandle)

    _GDIPlus_Startup()

    Local $aPos = WinGetPos($WinHandle)
    $iWidth = $aPos[2]
    $iHeight = $aPos[3]
    Local $hDDC = _WinAPI_GetDC($WinHandle)
    Local $hCDC = _WinAPI_CreateCompatibleDC($hDDC)

    $hBMP = _WinAPI_CreateCompatibleBitmap($hDDC, $iWidth, $iHeight)


    _WinAPI_SelectObject($hCDC, $hBMP)
    DllCall("User32.dll", "int", "PrintWindow", "hwnd", $WinHandle, "hwnd", $hCDC, "int", 0)
    _WinAPI_BitBlt($hCDC, 0, 0, $iWidth, $iHeight, $hDDC, 0, 0, $__SCREENCAPTURECONSTANT_SRCCOPY)


     $BMP = _GDIPlus_BitmapCreateFromHBITMAP($hBMP)
     Local $aPixelColor = _GDIPlus_BitmapGetPixel($BMP, $iX, $iY)

    _WinAPI_ReleaseDC($WinHandle, $hDDC)
    _WinAPI_DeleteDC($hCDC)
    _WinAPI_DeleteObject($hBMP)
   _GDIPlus_ImageDispose($BMP)
    _GDIPlus_Shutdown()

    Return Hex($aPixelColor, 6)
EndFunc   ;==>GetColor


And when we are at it, here is a little program to get the right color values for your program, just press Shift + X after configuration it, by putting in the right Program Title and you will get the coordinates and the color value of your mouse position inside of the program you wanted ^^

 

; #RequireAdmin Braucht Adminrechte falls ihr auf ein Programm mit Adminrechten zugreifen wollt!
#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <ScreenCapture.au3>
#include <Array.au3>


HotKeySet("{ESC}","_exit")
HotKeySet("X","Go")
Global $x
Global $y
Global $hBMP
;~ MsgBox(0,"","OK!")
$Title = "PUT THE TITLE OF YOUR WINDOW HERE!"
$hwnd = WinGetHandle($Title)
MsgBox (0,"",$hwnd)

While 1
Sleep (100)
WEnd

Func Go()
Local $MousePos = _WinAPI_GetMousePos(True, $hWnd)
$x = DllStructGetData($MousePos, "X")
$y = DllStructGetData($MousePos, "Y")
;~ $x = if you want to set them manual
;~ $y = use this and comment out both DLLStructGetData
$Color = GetColor($x, $y, $hwnd)
MsgBox(0,"","x: " &$x& " y: " &$y& " Color: " &$Color)
EndFunc

Func _Exit()
   Exit
EndFunc

Func GetColor($iX,$iY,$WinHandle)

    _GDIPlus_Startup()

    Local $aPos = WinGetPos($WinHandle)
    $iWidth = $aPos[2]
    $iHeight = $aPos[3]
    Local $hDDC = _WinAPI_GetDC($WinHandle)
    Local $hCDC = _WinAPI_CreateCompatibleDC($hDDC)

    $hBMP = _WinAPI_CreateCompatibleBitmap($hDDC, $iWidth, $iHeight)


    _WinAPI_SelectObject($hCDC, $hBMP)
    DllCall("User32.dll", "int", "PrintWindow", "hwnd", $WinHandle, "hwnd", $hCDC, "int", 0)
    _WinAPI_BitBlt($hCDC, 0, 0, $iWidth, $iHeight, $hDDC, 0, 0, $__SCREENCAPTURECONSTANT_SRCCOPY)


     $BMP = _GDIPlus_BitmapCreateFromHBITMAP($hBMP)
     Local $aPixelColor = _GDIPlus_BitmapGetPixel($BMP, $iX, $iY)

    _WinAPI_ReleaseDC($WinHandle, $hDDC)
    _WinAPI_DeleteDC($hCDC)
    _WinAPI_DeleteObject($hBMP)
   _GDIPlus_ImageDispose($BMP)
    _GDIPlus_Shutdown()

    Return Hex($aPixelColor, 6)
EndFunc   ;==>GetColor




I hope  I was able to help somebody on the long run :)
If you have any improvements or found any mistake, please let me know and I will try to improve the function as soon as possible.

Also please excuse my "not so clean" coding, I am still learning ;)


Have fun,
KryTonX
 

EDIT: Thanks at JohnOne for his improvements on the Script, I have added WinGetPos...I wonder why I never got that Idea :D

Edited by KryTonX

Share this post


Link to post
Share on other sites



Appears to do what it says on the tin.

;#RequireAdmin
#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <ScreenCapture.au3>
#include <Array.au3>

$scite = WinGetHandle("[ACTIVE]")
$gui = GUICreate("gui", 1000, 500)
GUISetBkColor(0x112233)
GUISetState()
WinActivate($scite)
Sleep(500)
$hwnd = WinGetHandle("gui")
MsgBox(0, 0, GetColor(100, 100, $hwnd))


Func GetColor($iX, $iY, $WinHandle)

    Local $aPos = WinGetPos($WinHandle)
    $iWidth = $aPos[2]
    $iHeight = $aPos[3]

    _GDIPlus_Startup()

    Local $hDDC = _WinAPI_GetDC($WinHandle)
    Local $hCDC = _WinAPI_CreateCompatibleDC($hDDC)

    $hBMP = _WinAPI_CreateCompatibleBitmap($hDDC, $iWidth, $iHeight)

    _WinAPI_SelectObject($hCDC, $hBMP)
    DllCall("User32.dll", "int", "PrintWindow", "hwnd", $WinHandle, "hwnd", $hCDC, "int", 0)
    _WinAPI_BitBlt($hCDC, 0, 0, $iWidth, $iHeight, $hDDC, 0, 0, $__SCREENCAPTURECONSTANT_SRCCOPY)

    $BMP = _GDIPlus_BitmapCreateFromHBITMAP($hBMP)
    Local $aPixelColor = _GDIPlus_BitmapGetPixel($BMP, $iX, $iY)

    _WinAPI_ReleaseDC($WinHandle, $hDDC)
    _WinAPI_DeleteDC($hCDC)
    _WinAPI_DeleteObject($hBMP)
    _GDIPlus_ImageDispose($BMP)

    Return Hex($aPixelColor, 6)
EndFunc   ;==>GetColor

Note: altered func params and does not require admin.

1 person likes this

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Thanks for your little improvement :)

I have added the change.

For me, somehow, it still doesn't give back the right ColorValue without Admin rights -_- I will let it in for now, if some people can confirm that it works without I will put it out!

 

Thanks for your help :)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Perhaps the window you are looking at is admin window, and that requires admin rights.

Anyway, might be prudent to shut down _GDIPlus_Startup().

Added :)

You are probably right with the Admin rights,

Fixed it in the code!

Edited by KryTonX

Share this post


Link to post
Share on other sites

Absolutely love this bro! Many thanks for the share and keep up the good work. :lol:

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Similar Content

    • JSmith312
      By JSmith312
      Hello Everyone! 
      I'm attempting to create a checklist app that performs other functions. For performance, I would like to check the box, then have the button to the right enabled. After that button is pressed, the second checkbox is enabled, and ready to be checked, allowing the second button to be pressed. After the second button is pressed, the completion/exit button is enabled/shown. I have my code that currently creates the GUI, creates the checkbox, but when you check it it enables the button and the next checkbox. Can't seem to figure out a Case for GUISetOnEvent. Any assistance would be appreciated!
      #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <StaticConstants.au3> #include <EditConstants.au3> #include <MsgBoxConstants.au3> ; Below is the GUI interface _DeploymentProcess() Func _DeploymentProcess() $aStep1 = GUICreate("Deployment Process", 475, 345, 500, 175) $CBcStep1 = GUICtrlCreateCheckbox("1. Step 01.", 15, 25, 300, 25) $BTNS1 = GUICtrlCreateButton("Email 01", 365, 25, 90, 20) $CBcStep2 = GUICtrlCreateCheckbox("2. Step 02.", 15, 50, 300, 25) $BTNS2 = GUICtrlCreateButton("Email 02", 365, 50, 90, 20) GUICtrlSetState($CBcStep2, $GUI_DISABLE) GUICtrlSetState($BTNS1, $GUI_DISABLE) $CBcStep3 = GUICtrlCreateButton("Hurray! You're Complete!", 85, 276, 300, 60) GUICtrlSetState($CBcStep3, $GUI_SHOW) ; Below are the button and checkbox enables/disables. GUISetState(@SW_SHOW) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE GUIDelete($aStep1) Return ; Step 1 Case $CBcStep1 If GUICtrlRead($CBcStep1) = $GUI_CHECKED Then GUICtrlSetState($BTNS1, $GUI_ENABLE) GUICtrlSetState($CBcStep2, $GUI_ENABLE) Else GUICtrlSetState($BTNS1, $GUI_DISABLE) GUICtrlSetState($CBcStep2, $GUI_DISABLE) EndIf ; Step 2 Case $CBcStep2 If GUICtrlRead($CBcStep2) = $GUI_CHECKED Then GUICtrlSetState($BTNS2, $GUI_ENABLE) GUICtrlSetState($CBcStep3, $GUI_ENABLE) Else GUICtrlSetState($BTNS2, $GUI_DISABLE) GUICtrlSetState($CBcStep3, $GUI_DISABLE) EndIf Case $CBcStep3 If GUICtrlRead($CBcStep2) = $GUI_CHECKED Then GUICtrlSetState($BTNS10, $GUI_ENABLE) GUICtrlSetState($CBcStep3, $GUI_SHOW) Else GUICtrlSetState($CBcStep3, $GUI_EVENT_CLOSE) EndIf Case $GUI_EVENT_CLOSE, $CBcStep3 #comments-end Exit EndSwitch WEnd EndFunc  
    • RoundChecker
      By RoundChecker
      Hi everyone,

      Is there a way to determine whether the script that is running, is already running, without using;
       
      If WinExists ?

      Or is there a way to use "If WinExists" to determine the script itself?

      The reason I am asking this is because if I want someone to run the .Exe I have and they rename it to whatever they want, how could I then determine in the script to check if there's already an .Exe open, or the script itself is already open?

      Should I use Class, or how do I achieve this?

      Thanks in advance.
    • svenjatzu
      By svenjatzu
      Id like to build a little helpertool for my boss in office but i dont know how to sart it.
      thats no complicated task. im working for an insurancecenter and have to search the customers in different orders and different tags.
      due to my boss is old and not really commen with pc he saves the customers without system.
      eg, customer andreas statham got an insurenca for his car then i got to search for
      andreas statham car, andreas car statham, car andreas statham, statham andreas car, statham car andreas, car statham andreass etc to find all the files from this customer in the harddrive.
      some customers also are fmiliarnamed in one file like andreas statam and monika rog are married an both got a carinsurance then i got to earch for
      andreas statham monika rog car, andreas statham monika car rog,.... this can take real long to find the right customer if i do it from hand.
      how can i realise this someone in here got an excamlple script or has any sugestion?
       
       
    • ur
      By ur
      When we run our exe from any service or through any background process, it won't interact with the applications running in the foreground, I mean launched when the user is logged in or in GUI mode.
       
      Let's say, the onedrive sync app can't be detected from the exe we created and running it in background.

      I tried both options below.

       
      Any Idea how to interact with foreground processes using AuotIT code.

      Other than telling to launch the exe also in foreground.
    • X_xkijux_x
      By X_xkijux_x
      Ok so im using this program. clarify for school. I would like to have a program that takes every word i type in microsoft word and searches on it in claryfi. is this possible. I would like to have the program not stopping me from typing more after that word. Like when i type a word it auto search it on clarify without making me stop typing and if i want to change i can do that and if im fine that word i can just keep typing and it will search after the next word.