Jump to content

Image search the same array element multiple times


DanBrill
 Share

Recommended Posts

Hi, new to AutoIt and a fairly novice programmer. Great program. I've (mostly) figured out ImageSearch.au3 thanks to examples I've found here, but one thing has me stumped.

I'm loading files from a directory into an array and then moving my mouse over them when the picture is matched. But it only seems to work once. If I reference an array element a second time it fails. Why? Does ImageSearchDLL.dll remove it from memory after it is referenced the first time?

Thanks for any help.

#include <ImageSearch.au3>
#include <File.au3>
#include <GDIPlus.au3>
#include <Array.au3> ;just for debug

;load these files to an array
Local $aFiles = _FileListToArrayRec(@ScriptDir & '\Pics\', '*.png', 1, 1, 0, 2)
If @error Then Exit

;debug files - display the array
;_ArrayDisplay($aFiles)

; Initialize GDI+ library
_GDIPlus_Startup()

; Turn the imported files into Bitmaps and put them in the Bitmaps array
Local $aBitmaps[$aFiles[0]]
Local $hImage = 0
For $i = 0 To UBound($aBitmaps) - 1
    $hImage = _GDIPlus_ImageLoadFromFile($aFiles[$i + 1])
    $aBitmaps[$i] = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    _GDIPlus_ImageDispose($hImage) ;Clean up resources
Next

; Set up the variables used by Array.au3
Local $iX = 0
Local $iY = 0
Local $iPic = 0

;Here I move the mouse over second picture from my Pic directory. Works fine.
$iPic = _ImageSearch($aBitmaps[1], 1, $iX, $iY, 20, 0)
If $iPic = 1 Then
    MouseMove($iX, $iY)
Else
    MsgBox(1, "Error", "Could not find image " & $aBitmaps[1] & @CRLF)
EndIf

; Now move the mouse to the first from the directory. Also fine.
$iPic = _ImageSearch($aBitmaps[0], 1, $iX, $iY, 20, 0)
If $iPic = 1 Then
    MouseMove($iX, $iY)
Else
    MsgBox(1, "Error", "Could not find image " & $aBitmaps[1] & @CRLF)
EndIf

; But try to go back to the second picture and it will fail.
; I can't seem to _ImageSearch this array element
; a second time. I'm not sure why.
$iPic = _ImageSearch($aBitmaps[1], 1, $iX, $iY, 20, 0)
If $iPic = 1 Then
    MouseMove($iX, $iY)
Else
    MsgBox(1, "Error", "Could not find image " & $aBitmaps[1] & @CRLF)
EndIf

;release the resources
For $i = 0 To UBound($aBitmaps) - 1
    _WinAPI_DeleteObject($aBitmaps[$i])
Next

; Shut down GDI+ library
_GDIPlus_Shutdown()

 

Link to comment
Share on other sites

Hi, welcome, may i ask, why do you load to bitmap?

#include <ImageSearch.au3>
#include <File.au3>
#include <GDIPlus.au3>
#include <Array.au3> ;just for debug

Global $HBMP = 0
Global $x1 = 0, $y1 = 0
Global $right = @DesktopWidth
Global $bottom = @DesktopHeight

;load these files to an array
Local $aFiles = _FileListToArrayRec(@ScriptDir & '\Pics\', '*.png', 1, 1, 0, 2)
If @error Then Exit

;debug files - display the array
_ArrayDisplay($aFiles)

$result = DllCall("ImageSearchDLL.dll","str","ImageSearch","int",$x1,"int",$y1,"int",$right,"int",$bottom,"str",$aFiles[1],"ptr",$HBMP)
    If $result[0]<>0 Then
        $array = StringSplit($result[0],"|")
        ConsoleWrite('XPos - '& $array[2] &' - YPos - '& $array[3] &' - XSize - '&$array[4]&' - YSize - '&$array[5]&@CRLF)
        MouseMove($array[2]+$array[4]/2, $array[3]+$array[5]/2)
    EndIf
$result = DllCall("ImageSearchDLL.dll","str","ImageSearch","int",$x1,"int",$y1,"int",$right,"int",$bottom,"str",$aFiles[2],"ptr",$HBMP)
    If $result[0]<>0 Then
        $array = StringSplit($result[0],"|")
        ConsoleWrite('XPos - '& $array[2] &' - YPos - '& $array[3] &' - XSize - '&$array[4]&' - YSize - '&$array[5]&@CRLF)
        MouseMove($array[2]+$array[4]/2, $array[3]+$array[5]/2)
    EndIf
$result = DllCall("ImageSearchDLL.dll","str","ImageSearch","int",$x1,"int",$y1,"int",$right,"int",$bottom,"str",$aFiles[1],"ptr",$HBMP)
    If $result[0]<>0 Then
        $array = StringSplit($result[0],"|")
        ConsoleWrite('XPos - '& $array[2] &' - YPos - '& $array[3] &' - XSize - '&$array[4]&' - YSize - '&$array[5]&@CRLF)
        MouseMove($array[2]+$array[4]/2, $array[3]+$array[5]/2)
    EndIf

 

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

The reason I loaded the files to bitmap was because I thought you had to do that to convert .png, .jpg, etc. to a bitmap first. Maybe I'm wrong about that.

So it doesn't appear that you are using ImageSearch.au3 at all but going directly to the DLL, which is of course the real workhorse. Interesting. In any case, your script isn't working for me. It loads the files into the array, but the DLL call doesn't appear to do anything and $result doesn't seem to get populated. 

And I'm still not sure about my original question: why could I only search the image from my $aBitmaps one time?

Thanks for the ideas. I'm going to study this some more.

 

 

Link to comment
Share on other sites

Im not sure what to say, it should work, does here.

As for the question, im lost too, i think it should work, but i could not test it as it fails to find even the first one.

Edited by careca
Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

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