Jump to content

PixelSearch problem


NoHAX
 Share

Recommended Posts

Dunno if this will work for you, but you could make it create a box in the middle of the screen then expand out through a loop until it finds the pixel color your looking for.

basically you would have a loop and then the pixel search, the x and y and height and width would be relative to the increment and the total image size.

So you would need to calculate the pixel search box each time but it would allow you to "Focus" on a specific spot of the window before looking at a different spot.

Posted Image

If you need it to be faster, make the iterations jump more then 1 pixel.

Good luck,

Ilmaestro.

Hello, World!... LAME lol

Link to comment
Share on other sites

This example uses PixelSearch() functions to find the second or next instance of a pixel being searched for.

#include <Array.au3>
#include <GDIPlus.au3>
#include <WindowsConstants.au3>
#include <GuiConstantsEx.au3>

Opt("PixelCoordMode", 2)        ;1=absolute, 0=relative, 2=client

Local $i_ColourToFind = 0xFF7F27 ; In RGB hex format(0xRRGGBB)
Local $i_ShadeVariation = 5 ; ±5 of 0xFF7F27
Local $i_FindInstance = 2

Local $hWhnd = GUICreate("", 100, 87, 10, 10, $WS_POPUPWINDOW, $WS_EX_TOPMOST)

_GDIPlus_Startup()
Local $hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\beztytuujn.png") ; Copied from Post #1
Local $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hWhnd)

GUIRegisterMsg($WM_PAINT, "MY_WM_PAINT")
GUISetState()

; Find 1st instance of $i_ColourToFind
Local $aCoord1stFind = PixelSearch(0, 0, 100, 87, $i_ColourToFind, $i_ShadeVariation); ,1,$hWhnd)
If @error Then
    MsgBox(0, "Results", $i_ColourToFind & " colour could not be found at all.")
    Exit
Else ; Search for 2nd $i_ColourToFind pixel.
    local $sText = "1st instance of 0x" & Hex($i_ColourToFind, 6) & " pixel is at X, Y coordinates: " & _
                $aCoord1stFind[0] & "," & $aCoord1stFind[1] & @CRLF

    $aCoord2nd = _PixelSearchNextInstance($aCoord1stFind, 0, 0, 100, 87, $i_ColourToFind, $i_ShadeVariation)
    If Not @error Then
        MsgBox(0, "Results", $sText & "2nd instance of 0x" & Hex($i_ColourToFind, 6) & _
                " pixel is at X, Y coordinates: " & $aCoord2nd[0] & "," & $aCoord2nd[1])
    Else ;Could not find 2nd instance of $i_ColourToFind pixel.
        MsgBox(0, "Results", $sText & "2nd instance of 0x" & Hex($i_ColourToFind, 6) & " pixel NOT found.")
    EndIf
EndIf


; Clean up resources
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_Shutdown()


; Parameters:-
;   $aCoord1stFind - Array containing X,Y coordinates of previous pixel found.
;   $iLeft      - Left coordinate of rectangle.
;   $iTop       - Top coordinate of rectangle.
;   $iRight     - Right coordinate of rectangle.
;   $iBottom  - Bottom coordinate of rectangle.
;   $iColor     - Colour value of pixel to find (in decimal or hex).
;   $iShadeVariation [optional] - A number between 0 and 255 to indicate the allowed number of shades of
;                       variation of the red, green, and blue components of the colour. Default is 0 (exact match).
;
Func _PixelSearchNextInstance($aCoord1stFind, $iLeft, $iTop, $iRight, $iBottom, $iColor, $iShadeVariation = 0)

    ; Check the rest of the line for 2nd instance of $iColor
    Local $coord = PixelSearch($aCoord1stFind[0] + 1, $aCoord1stFind[1], $iRight, $aCoord1stFind[1], $iColor, $iShadeVariation)
    If Not @error Then
        Return SetError(0, 0, $coord)
    Else ; 2nd instance Not on same line.

        ; Check all remaining lines for 2nd instance
        Local $coord = PixelSearch($iLeft, $aCoord1stFind[1] + 1, $iRight, $iBottom, $iColor, $iShadeVariation)
        If Not @error Then
            Return SetError(0, 0, $coord)
        Else ;Could not find 2nd instance of pixel.
            Return SetError(1, 0, 1); Set error to 1
        EndIf
    EndIf
EndFunc   ;==>_PixelSearchNextInstance

; Draw PNG image
Func MY_WM_PAINT($hWnd, $Msg, $wParam, $lParam)
    _WinAPI_RedrawWindow($hWhnd, 0, 0, $RDW_UPDATENOW)
    _GDIPlus_GraphicsDrawImage($hGraphic, $hImage, 0, 0)
    _WinAPI_RedrawWindow($hWhnd, 0, 0, $RDW_VALIDATE)
    Return $GUI_RUNDEFMSG
EndFunc   ;==>MY_WM_PAINT

; Results are:-
; 1st instance of 0xFF7F27 pixel is at X, Y coordinates: 13,13
; 2nd instance of 0xFF7F27 pixel is at X, Y coordinates: 61,37

; To check uncomment the following.
;ConsoleWrite("First  :0x" & hex(PixelGetColor(13,13,$hWhnd),6) & @CRLF)
;ConsoleWrite("BkGnd  :0x" & hex(PixelGetColor(60,37,$hWhnd),6) & @CRLF)
;ConsoleWrite("Second :0x" & hex(PixelGetColor(61,37,$hWhnd),6) & @CRLF)
;ConsoleWrite("BkGnd  :0x" & hex(PixelGetColor(62,37,$hWhnd),6) & @CRLF)
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...