Help with ImageSearch Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Tampleeeee
Posts: 19
Joined: 12 Oct 2019, 03:25

Help with ImageSearch

30 Oct 2019, 02:30

Hello everyone, I need help with ImageSearch. I have a PNG file in the folder D: \ Game Logo.png, I need the code to look for this image and then the mouse move to it well, let's say the MouseMove command. I tried to do something, but unfortunately nothing works, I want to ask you for help. I hope you can help me. Thanks for your attention.
SOTE
Posts: 1014
Joined: 15 Jun 2015, 06:21

Re: Help with ImageSearch

30 Oct 2019, 03:49

Probably you want something like below:

Code: Select all

; Control F to activate the script.
^f::
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *12 D:\Game Logo.png
if ErrorLevel = 2
{
	MsgBox Could not conduct the search.
}
if ErrorLevel = 1
{
	MsgBox could not be found on the screen.
}
if ErrorLevel = 0
{
	MsgBox,, Information, The image was found at %FoundX%x%FoundY%`n Don't touch mouse as app will move it, 5
	MouseMove, %FoundX%, %FoundY%
}
Exit
https://www.autohotkey.com/docs/commands/ImageSearch.htm
(ImageSearch)

https://www.autohotkey.com/docs/commands/MouseMove.htm
(MouseMove)

Where people sometimes have problems, is with using the options. The "*12" before the location of the file (D: \ Game Logo.png) allows for shades of variation. It's often better to use this, and then narrow the range down to the minimum acceptable, if it has trouble finding the image on screen.

Edited.
Last edited by SOTE on 30 Oct 2019, 07:14, edited 2 times in total.
Tampleeeee
Posts: 19
Joined: 12 Oct 2019, 03:25

Re: Help with ImageSearch

30 Oct 2019, 05:10

probably it's doesn't work
User avatar
boiler
Posts: 4605
Joined: 21 Dec 2014, 02:44

Re: Help with ImageSearch

30 Oct 2019, 05:19

At least one issue is this line. There cannot be spaces around the \ in the file name D: \ Game Logo.png. It needs to be D:\Game Logo.png

Code: Select all

ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *12 D: \ Game Logo.png
On another note, the following statement doesn't affect the suggested script. However...
SOTE wrote:
30 Oct 2019, 03:49
If the mouse doesn't move to exactly the location that you want, MouseMove has a Relative option, where you can specific an offset to move the pointer up or down from the original location.
I'm not seeing how using the Relative option of MouseMove will ever be able to move the cursor to the found image as desired. There are no circumstances where the found coordinates are provided relative to the mouse location, only relative to the screen or the active window.
User avatar
Hellbent
Posts: 1026
Joined: 23 Sep 2017, 13:34

Re: Help with ImageSearch

30 Oct 2019, 05:29

Try running this. It might reveal a problem other than your imageSearch code.

Steps:

1: Run the script.
2: Press "Take Screen Shot" and then capture a small area of the app that doesn't change.
3: Press "Set Search Area" and set a area that contains the shot you took.
4: Press "Search" and wait a few seconds to see if your cursor moves.

Code: Select all

;Written By: Hellbent aka CivReborn
;Date: June 15th 2019
#SingleInstance,Force
SetBatchLines,-1
IfNotExist,%A_ScriptDir%\Recorded Shots
	FileCreateDir, %A_ScriptDir%\Temp Screen Shots
SetWorkingDir,%A_ScriptDir%\Temp Screen Shots
Coordmode,Mouse,screen
CoordMode,Pixel,Screen
global CapActive:=0,SP,XCAP,YCAP,WCAP,HCAP,OB:=0,CAPIT:=0
global CapWin:={},Monitors := New MonitorClass()
Gdip_Startup()
global TXC,TYC,BB:=New_Brush("000000","88"),GB:=New_Brush("00ff00","55"),RB:=New_Brush("ff0000","55")


Gui,1:+AlwaysOnTop -DPIScale +HWNDGUI1HWND
Gui,1:Color,333333,444444
Gui,1:Font,cwhite s10 Bold ,Segoe UI
Gui,1:Add,Button,xm ym w300 h30 -Theme gSet_Capture_Area vSCAP,Take Screen Shot
Gui,1:Add,Button,xm w300 h30 -Theme gSet_Capture_Area vSSA,Set Search Area
Gui,1:Add,Button,xm w300 h30 -Theme gImageSearch,Search
Gui,1:Add,Picture,xm y+10 w300 h300 vPic gUpdatePic,% A_WorkingDir "\Test Screen Shot.png"
UpdatePic()
Gui,1:Show,AutoSize,HB Snap it
return
GuiClose:
GuiContextMenu:
2GuiContextMenu:
*ESC::
	ExitApp
	
UpdatePic(){
	static FakePic
	Gui,Fake:Add,Picture,vFakePic,% A_WorkingDir "\Test Screen Shot.png"
	GuiControlGet,Pos,Fake:Pos,FakePic
	Gui,Fake:Destroy
	if(PosW<=300&&PosH<=300){
		NW:=PosW,NH:=PosH
	}else if(PosW>PosH){
		ratio:=PosH/PosW,NW:=300,NH:=300*Ratio
	}else if(PosW<PosH){
		ratio:=PosW/PosH,NH:=300,NW:=300*Ratio
	}else	{
		NW:=300,NH:=300
	}
	GuiControl,1:Move,Pic,% "w" NW " h" NH
	GuiControl,1:,Pic,% A_WorkingDir "\Test Screen Shot.png"
	Gui,1:Show,AutoSize
}

Set_Capture_Area:
	if(A_GuiControl="SCAP")
		CAPIT:=1
	Create_Capture_Size_Window()
	return
	
Numpad3::	
ImageSearch:
	ImageSearch,foundx,foundy,XCAP,YCAP,XCAP+WCAP,YCAP+HCAP,%A_WorkingDir%\Test Screen Shot.png
	if(ErrorLevel=0){
		SoundBeep,500
		MouseMove,FoundX+20,FoundY+20
	}else if(ErrorLevel=1){
		Loop 3
			SoundBeep,600
	}
	return

Create_Capture_Size_Window(){
	static ft
	Gui,2:Destroy
	CapWin:=Layered_Window_SetUp(3,0,0,A_ScreenWidth,A_ScreenHeight,"2","+AlwaysOnTop -DPIScale -Caption +Owner1")
	Max:=Monitors.Get_Largest_Monitor_Size()
	CapWin.hbm:=CreateDIBSection(Max.Width,Max.Height)
	CapWin.hdc := CreateCompatibleDC()
	CapWin.obm := SelectObject(CapWin.hdc,CapWin.hbm),CapWin.G := Gdip_GraphicsFromHDC(CapWin.hdc)
	Gdip_SetSmoothingMode(CapWin.G,3)

	;~ ToolTip,% Max.Width "`n" Max.Height
	if(!ft){
		Monitors.Set_Window_Move_Timer(GUINAME:=2,GUIHWND:=CapWin.hwnd,TCount:=300,xpOff:=0,ypOff:=0,xr:=1,yr:=1,Fill_Screen:=1)
		ft:=1
	}else 	{
		Monitors.Window_Move_Obj.GUIHWND:=CapWin.hwnd
		Monitors.Turn_On_Window_Move_Timer()
	}
	CapActive:=1,SP:=0,OB:=0
	SetTimer,Set_Positions,10
}

Set_Positions(){
	Coordmode,Mouse,Window
	If(!WinActive("ahk_Id" CapWin.Hwnd)){
		WinActivate,% "ahk_Id" CapWin.Hwnd
	}
	Gdip_GraphicsClear(CapWin.g)
	MouseGetPos,tx,ty
	if(SP=0){
		Fill_Box(CapWin.G,BB,tx,0,1,Monitors.Monitor[Monitors.Window_Move_Obj.Current_Monitor].Bottom-Monitors.Monitor[Monitors.Window_Move_Obj.Current_Monitor].Top)
		Fill_Box(CapWin.G,BB,0,ty,Monitors.Monitor[Monitors.Window_Move_Obj.Current_Monitor].Right-Monitors.Monitor[Monitors.Window_Move_Obj.Current_Monitor].Left,1)
		TXC:=tx,TYC:=ty
	}else if(SP=1){
		;~ Monitors.Turn_Off_Window_Move_Timer()
		Fill_Box(CapWin.G,BB,TXC,0,1,Monitors.Monitor[Monitors.Window_Move_Obj.Current_Monitor].Bottom-Monitors.Monitor[Monitors.Window_Move_Obj.Current_Monitor].Top)
		Fill_Box(CapWin.G,BB,0,TYC,Monitors.Monitor[Monitors.Window_Move_Obj.Current_Monitor].Right-Monitors.Monitor[Monitors.Window_Move_Obj.Current_Monitor].Left,1)
		Fill_Box(CapWin.G,BB,tx,0,1,Monitors.Monitor[Monitors.Window_Move_Obj.Current_Monitor].Bottom-Monitors.Monitor[Monitors.Window_Move_Obj.Current_Monitor].Top)
		Fill_Box(CapWin.G,BB,0,ty,Monitors.Monitor[Monitors.Window_Move_Obj.Current_Monitor].Right-Monitors.Monitor[Monitors.Window_Move_Obj.Current_Monitor].Left,1)
		if(TXC<tx&&TYC<ty){
			Fill_Box(CapWin.G,GB,TXC,TYC,tx-TXC,ty-TYC)
			WCAP:=tx-TXC,HCAP:=ty-TYC,OB:=0
			XCAP:=TXC+Monitors.Monitor[Monitors.Window_Move_Obj.Current_Monitor].Left,YCAP:=TYC+Monitors.Monitor[Monitors.Window_Move_Obj.Current_Monitor].Top
		}else if(TXC>tx&&TYC<ty){
			Fill_Box(CapWin.G,RB,tx,TYC,TXC-tx,ty-TYC)
			OB:=1
		}else if(TXC>tx&&TYC>ty){
			Fill_Box(CapWin.G,RB,tx,ty,TXC-tx,TYC-ty)
			OB:=1
		}else if(TXC<tx&&TYC>ty){
			Fill_Box(CapWin.G,RB,TXC,ty,tx-TXC,TYC-ty)
			OB:=1
		}
	}
	UpdateLayeredWindow(CapWin.hwnd, CapWin.hdc,Monitors.Monitor[Monitors.Window_Move_Obj.Current_Monitor].Left,Monitors.Monitor[Monitors.Window_Move_Obj.Current_Monitor].Top,Monitors.Monitor[Monitors.Window_Move_Obj.Current_Monitor].Right-Monitors.Monitor[Monitors.Window_Move_Obj.Current_Monitor].Left, Monitors.Monitor[Monitors.Window_Move_Obj.Current_Monitor].Bottom-Monitors.Monitor[Monitors.Window_Move_Obj.Current_Monitor].Top)
}

#IF (CapActive=1)
	LButton::
		if(SP=0)
			SP:=1
		else if(SP=1&&OB=0){
			Monitors.Turn_Off_Window_Move_Timer()
			CapActive:=0
			SelectObject(CapWin.hdc,CapWin.obm),DeleteObject(CapWin.hbm),DeleteDC(CapWin.hdc),gdip_deleteGraphics(CapWin.g)
			CapWin:="",SP:=0
			Gui,2:Destroy
			SetTimer,Set_Positions,Off
			if(CAPIT)
				gosub,Take_ScreenShot
		}
		return
#IF

Numpad1::
	SelectObject(CapWin.hdc,CapWin.obm),DeleteObject(CapWin.hbm),DeleteDC(CapWin.hdc),gdip_deleteGraphics(CapWin.g)
	CapWin:="",SP:=0,CapActive:=0,CAPIT:=1
	Create_Capture_Size_Window()
	return
	
Numpad2::
	SelectObject(CapWin.hdc,CapWin.obm),DeleteObject(CapWin.hbm),DeleteDC(CapWin.hdc),gdip_deleteGraphics(CapWin.g)
	CapWin:="",SP:=0,CapActive:=0,CAPIT:=0
	Create_Capture_Size_Window()
	return

Take_ScreenShot:
	CAPIT:=0
	TempBitmap:=Gdip_BitmapFromScreen(XCAP "|" YCAP "|" WCAP "|" HCAP)
	Gdip_SaveBitmapToFile(TempBitmap,A_WorkingDir "\Test Screen Shot.png", 100)
	Gdip_DisposeImage(TempBitmap)
	SoundBeep,400,300
	UpdatePic()
	return	

;-------------------------------------------------------------------------------------------------
;-------------------------------------------------------------------------------------------------
;-------------------------------------------------------------------------------------------------

class MonitorClass	{
	__New(){
		This._SetMonCount()
		This._SetPrimeMonitor()
		This._Set_Bounds()
	}
	_SetMonCount(){
		local MC
		SysGet, MC, MonitorCount 
		This.MonitorCount := MC
	}
	_SetPrimeMonitor(){
		local PM
		SysGet, PM, MonitorPrimary
		This.PrimeMonitor := PM
	}
	_Set_Bounds(){
		local bmon,bmonLeft,bmonRight,bmonTop,bmonBottom
		This.Monitors:=[]
		Loop,% This.MonitorCount	{
			SysGet, bmon, Monitor, % A_Index
			This.Monitor[A_Index]:=	{ Left		: 	bmonLeft
									, Top		: 	bmonTop
									, Right		: 	bmonRight
									, Bottom	: 	bmonBottom 	}
		}
	}
	Get_Current_Monitor(){
		local x,y
		CoordMode,Mouse,Screen
		MouseGetPos,x,y
		Loop,% This.MonitorCount	{
			if(x>=This.Monitor[A_Index].Left&&x<=This.Monitor[A_Index].Right&&y>=This.Monitor[A_Index].Top&&y<=This.Monitor[A_Index].Bottom){
				return A_Index
			}
		}
	}
	Get_New_Window_Position(curMon,xpOff,ypOff,xr,yr){
		local tposa:="",tposa:={}
		if(xr=1) ; 1 xr = relative to the left side ; 2 xr = relative to the right side
			tposa.x:=This.Monitor[curMon].Left+xpOff
		else 
			tposa.x:=This.Monitor[curMon].Right-xpOff
		if(yr=1) ; 1 yr = relative to the top  ; 2 yr = relative to the bottom
			tposa.y:=This.Monitor[curMon].Top+ypOff
		else 
			tposa.y:=This.Monitor[curMon].Bottom-ypOff
		return tposa
	}
	Set_Window_Move_Timer(GUINAME:=1,GUIHWND:="",TCount:=500,xpOff:=0,ypOff:=0,xr:=1,yr:=1,Fill_Screen:=0){
		
		local Window_Timer
		
		This.Window_Move_Obj:=	{ 	Interval		:	TCount
								,	GUINAME			:	GUINAME
								,	FILLSCREEN		:	Fill_Screen
								,	GUIHWND			:	GUIHWND
								,	XPOFF			:	xpOff
								,	YPOFF			:	ypOff
								,	XRelative		:	xr
								,	YRelative		:	yr	
								,	Current_Monitor	:	This.Get_Current_Monitor()
								,	Old_Monitor		:	This.Get_Current_Monitor()	
								,	NEW_GUI_POS		:	This.Get_New_Window_Position(This.Get_Current_Monitor(),xpOff,ypOff,xr,yr)	}
								
		This.Window_Timer := Window_Timer :=  ObjBindMethod(This, "_Window_Move_Timer")
		
		This._Set_TFTime()
		
		SetTimer,%Window_Timer%,%TCount%

	}
	Get_Largest_Monitor_Size(){
		local tempMSW:="",tempMSH:=""
		Loop,% This.MonitorCount	{
			if(This.Monitor[A_Index].Right-This.Monitor[A_Index].Left>=This.Monitor[A_Index+1].Right-This.Monitor[A_Index+1].Left&&This.Monitor[A_Index].Right-This.Monitor[A_Index].Left>=This.Monitor[tempMSW].Right-This.Monitor[tempMSW].Left)
				tempMSW:=A_index
			if(This.Monitor[A_Index].Bottom-This.Monitor[A_Index].Top>=This.Monitor[A_Index+1].Bottom-This.Monitor[A_Index+1].Top&&This.Monitor[A_Index].Bottom-This.Monitor[A_Index].Top>=This.Monitor[tempMSH].Bottom-This.Monitor[tempMSH].Top)
				tempMSH:=A_index
		}
		This.Max_Monitor_Dimensions := {Width: This.Monitor[tempMSW].Right-This.Monitor[tempMSW].Left, Height: This.Monitor[tempMSH].Bottom-This.Monitor[tempMSH].Top}
		return This.Max_Monitor_Dimensions
	}
	_Window_Move_Timer(){
		This.Window_Move_Obj.Current_Monitor := This.Get_Current_Monitor()
		if(This.Window_Move_Obj.Current_Monitor!=This.Window_Move_Obj.Old_Monitor&&!DllCall("IsIconic", "Ptr", This.Window_Move_Obj.GUIHWND, "UInt")){
			This.Window_Move_Obj.NEW_GUI_POS:=This.Get_New_Window_Position(This.Window_Move_Obj.Current_Monitor,This.Window_Move_Obj.XPOFF,This.Window_Move_Obj.YPOFF,This.Window_Move_Obj.XRelative,This.Window_Move_Obj.YRelative)
			This._Move_Window()
			This.Window_Move_Obj.Old_Monitor := This.Window_Move_Obj.Current_Monitor
		}
	}
	_Set_TFTime(){
		This.Window_Move_Obj.Current_Monitor := This.Get_Current_Monitor()
		This.Window_Move_Obj.NEW_GUI_POS:=This.Get_New_Window_Position(This.Window_Move_Obj.Current_Monitor,This.Window_Move_Obj.XPOFF,This.Window_Move_Obj.YPOFF,This.Window_Move_Obj.XRelative,This.Window_Move_Obj.YRelative)
		This.Window_Move_Obj.Old_Monitor := This.Window_Move_Obj.Current_Monitor
		This._Move_Window()
	}
	_Move_Window(){
		if(!This.Window_Move_Obj.FILLSCREEN)
			Gui,% This.Window_Move_Obj.GUINAME ":Show",% "x" This.Window_Move_Obj.NEW_GUI_POS.X " y" This.Window_Move_Obj.NEW_GUI_POS.Y " NA"
		else
			Gui,% This.Window_Move_Obj.GUINAME ":Show",% "x" This.Window_Move_Obj.NEW_GUI_POS.X " y" This.Window_Move_Obj.NEW_GUI_POS.Y " w" This.Monitor[This.Window_Move_Obj.Current_Monitor].Right " h" This.Monitor[This.Window_Move_Obj.Current_Monitor].Bottom " NA"
	}
	Turn_Off_Window_Move_Timer(){
		local Window_Timer
		Window_Timer := This.Window_Timer
		SetTimer,%Window_Timer%,Off
	}
	Turn_On_Window_Move_Timer(){
		local Window_Timer
		Window_Timer := This.Window_Timer
		This._Set_TFTime()
		SetTimer,%Window_Timer%,On
	}
	GetGuiPos(){
		local x,y
		WinGetPos,x,y,,,% "ahk_id " This.Window_Move_Obj.GUIHWND 
		return x
	}
}
;######################################################################################################################################
;#####################################################   					    #######################################################
;#####################################################  	  Gdip LITE		    #######################################################
;#####################################################  					    #######################################################
;######################################################################################################################################
Layered_Window_SetUp(Smoothing,Window_X,Window_Y,Window_W,Window_H,Window_Name:=1,Window_Options:=""){
	Layered:={}
	Layered.W:=Window_W,Layered.H:=Window_H,Layered.X:=Window_X,Layered.Y:=Window_Y
	Layered.Name:=Window_Name,Layered.Options:=Window_Options
	;~ Layered.Token:=Gdip_Startup()
	Create_Layered_GUI(Layered),Layered.hwnd:=winExist()
	;~ Layered.hbm := CreateDIBSection(Window_W,Window_H),Layered.hdc := CreateCompatibleDC()
	;~ Layered.obm := SelectObject(Layered.hdc,Layered.hbm),Layered.G := Gdip_GraphicsFromHDC(Layered.hdc)
	;~ Gdip_SetSmoothingMode(Layered.G,Smoothing)
	return Layered
}
Create_Layered_GUI(Layered){
	Gui,% Layered.Name ": +E0x80000 +LastFound " Layered.Options 
	Gui,% Layered.Name ":Show",% "x" Layered.X " y" Layered.Y " w" Layered.W " h" Layered.H ;" NA"
}	
Layered_Window_ShutDown(This){
	SelectObject(This.hdc,This.obm),DeleteObject(This.hbm),DeleteDC(This.hdc)
	gdip_deleteGraphics(This.g),Gdip_Shutdown(This.Token)
}
New_Brush(colour:="000000",Alpha:="FF"){
	new_colour := "0x" Alpha colour 
	return Gdip_BrushCreateSolid(new_colour)
}
New_Pen(colour:="000000",Alpha:="FF",Width:= 5){
	new_colour := "0x" Alpha colour 
	return Gdip_CreatePen(New_Colour,Width)
}	
Fill_Box(pGraphics,pBrush,x,y,w,h)	{
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	return DllCall("gdiplus\GdipFillRectangle", Ptr, pGraphics, Ptr, pBrush, "float", x, "float", y, "float", w, "float", h)
}
; Gdip standard library v1.45 by tic (Tariq Porter) 07/09/11
; Modifed by Rseding91 using fincs 64 bit compatible Gdip library 5/1/2013
BitBlt(ddc, dx, dy, dw, dh, sdc, sx, sy, Raster=""){
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	return DllCall("gdi32\BitBlt", Ptr, dDC, "int", dx, "int", dy, "int", dw, "int", dh, Ptr, sDC, "int", sx, "int", sy, "uint", Raster ? Raster : 0x00CC0020)
}
Gdip_SetImageAttributesColorMatrix(Matrix){
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	VarSetCapacity(ColourMatrix, 100, 0)
	Matrix := RegExReplace(RegExReplace(Matrix, "^[^\d-\.]+([\d\.])", "$1", "", 1), "[^\d-\.]+", "|")
	StringSplit, Matrix, Matrix, |
	Loop, 25
	{
		Matrix := (Matrix%A_Index% != "") ? Matrix%A_Index% : Mod(A_Index-1, 6) ? 0 : 1
		NumPut(Matrix, ColourMatrix, (A_Index-1)*4, "float")
	}
	DllCall("gdiplus\GdipCreateImageAttributes", A_PtrSize ? "UPtr*" : "uint*", ImageAttr)
	DllCall("gdiplus\GdipSetImageAttributesColorMatrix", Ptr, ImageAttr, "int", 1, "int", 1, Ptr, &ColourMatrix, Ptr, 0, "int", 0)
	return ImageAttr
}
Gdip_GetImageWidth(pBitmap){
   DllCall("gdiplus\GdipGetImageWidth", A_PtrSize ? "UPtr" : "UInt", pBitmap, "uint*", Width)
   return Width
}
Gdip_GetImageHeight(pBitmap){
   DllCall("gdiplus\GdipGetImageHeight", A_PtrSize ? "UPtr" : "UInt", pBitmap, "uint*", Height)
   return Height
}
Gdip_DeletePen(pPen){
   return DllCall("gdiplus\GdipDeletePen", A_PtrSize ? "UPtr" : "UInt", pPen)
}
Gdip_DeleteBrush(pBrush){
   return DllCall("gdiplus\GdipDeleteBrush", A_PtrSize ? "UPtr" : "UInt", pBrush)
}
Gdip_DisposeImage(pBitmap){
   return DllCall("gdiplus\GdipDisposeImage", A_PtrSize ? "UPtr" : "UInt", pBitmap)
}
Gdip_DeleteGraphics(pGraphics){
   return DllCall("gdiplus\GdipDeleteGraphics", A_PtrSize ? "UPtr" : "UInt", pGraphics)
}
Gdip_DisposeImageAttributes(ImageAttr){
	return DllCall("gdiplus\GdipDisposeImageAttributes", A_PtrSize ? "UPtr" : "UInt", ImageAttr)
}
CreateCompatibleDC(hdc=0){
   return DllCall("CreateCompatibleDC", A_PtrSize ? "UPtr" : "UInt", hdc)
}
SelectObject(hdc, hgdiobj){
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	return DllCall("SelectObject", Ptr, hdc, Ptr, hgdiobj)
}
DeleteObject(hObject){
   return DllCall("DeleteObject", A_PtrSize ? "UPtr" : "UInt", hObject)
}
GetDC(hwnd=0){
	return DllCall("GetDC", A_PtrSize ? "UPtr" : "UInt", hwnd)
}
GetDCEx(hwnd, flags=0, hrgnClip=0){
	Ptr := A_PtrSize ? "UPtr" : "UInt"
    return DllCall("GetDCEx", Ptr, hwnd, Ptr, hrgnClip, "int", flags)
}
ReleaseDC(hdc, hwnd=0){
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	return DllCall("ReleaseDC", Ptr, hwnd, Ptr, hdc)
}
DeleteDC(hdc){
   return DllCall("DeleteDC", A_PtrSize ? "UPtr" : "UInt", hdc)
}
Gdip_SetClipRegion(pGraphics, Region, CombineMode=0){
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	return DllCall("gdiplus\GdipSetClipRegion", Ptr, pGraphics, Ptr, Region, "int", CombineMode)
}
CreateDIBSection(w, h, hdc="", bpp=32, ByRef ppvBits=0){
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	hdc2 := hdc ? hdc : GetDC()
	VarSetCapacity(bi, 40, 0)
	NumPut(w, bi, 4, "uint"), NumPut(h, bi, 8, "uint"), NumPut(40, bi, 0, "uint"), NumPut(1, bi, 12, "ushort"), NumPut(0, bi, 16, "uInt"), NumPut(bpp, bi, 14, "ushort")
	hbm := DllCall("CreateDIBSection", Ptr, hdc2, Ptr, &bi, "uint", 0, A_PtrSize ? "UPtr*" : "uint*", ppvBits, Ptr, 0, "uint", 0, Ptr)
	if !hdc
		ReleaseDC(hdc2)
	return hbm
}
Gdip_GraphicsFromHDC(hdc){
    DllCall("gdiplus\GdipCreateFromHDC", A_PtrSize ? "UPtr" : "UInt", hdc, A_PtrSize ? "UPtr*" : "UInt*", pGraphics)
    return pGraphics
}
Gdip_GetDC(pGraphics){
	DllCall("gdiplus\GdipGetDC", A_PtrSize ? "UPtr" : "UInt", pGraphics, A_PtrSize ? "UPtr*" : "UInt*", hdc)
	return hdc
}
Gdip_Startup(){
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	if !DllCall("GetModuleHandle", "str", "gdiplus", Ptr)
		DllCall("LoadLibrary", "str", "gdiplus")
	VarSetCapacity(si, A_PtrSize = 8 ? 24 : 16, 0), si := Chr(1)
	DllCall("gdiplus\GdiplusStartup", A_PtrSize ? "UPtr*" : "uint*", pToken, Ptr, &si, Ptr, 0)
	return pToken
}
Gdip_CreatePen(ARGB, w){
   DllCall("gdiplus\GdipCreatePen1", "UInt", ARGB, "float", w, "int", 2, A_PtrSize ? "UPtr*" : "UInt*", pPen)
   return pPen
}
Gdip_BrushCreateSolid(ARGB=0xff000000){
	DllCall("gdiplus\GdipCreateSolidFill", "UInt", ARGB, A_PtrSize ? "UPtr*" : "UInt*", pBrush)
	return pBrush
}
Gdip_BrushCreateHatch(ARGBfront, ARGBback, HatchStyle=0){
	DllCall("gdiplus\GdipCreateHatchBrush", "int", HatchStyle, "UInt", ARGBfront, "UInt", ARGBback, A_PtrSize ? "UPtr*" : "UInt*", pBrush)
	return pBrush
}
CreateRectF(ByRef RectF, x, y, w, h){
   VarSetCapacity(RectF, 16)
   NumPut(x, RectF, 0, "float"), NumPut(y, RectF, 4, "float"), NumPut(w, RectF, 8, "float"), NumPut(h, RectF, 12, "float")
}
CreatePointF(ByRef PointF, x, y){
   VarSetCapacity(PointF, 8)
   NumPut(x, PointF, 0, "float"), NumPut(y, PointF, 4, "float")
}
Gdip_FillRectangle(pGraphics, pBrush, x, y, w, h){
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	return DllCall("gdiplus\GdipFillRectangle", Ptr, pGraphics, Ptr, pBrush, "float", x, "float", y, "float", w, "float", h)
}
Gdip_SetClipRect(pGraphics, x, y, w, h, CombineMode=0){
   return DllCall("gdiplus\GdipSetClipRect",  A_PtrSize ? "UPtr" : "UInt", pGraphics, "float", x, "float", y, "float", w, "float", h, "int", CombineMode)
}
Gdip_ResetClip(pGraphics){
   return DllCall("gdiplus\GdipResetClip", A_PtrSize ? "UPtr" : "UInt", pGraphics)
}
Gdip_SetSmoothingMode(pGraphics, SmoothingMode){
   return DllCall("gdiplus\GdipSetSmoothingMode", A_PtrSize ? "UPtr" : "UInt", pGraphics, "int", SmoothingMode)
}
Gdip_GraphicsClear(pGraphics, ARGB=0x00ffffff){
    return DllCall("gdiplus\GdipGraphicsClear", A_PtrSize ? "UPtr" : "UInt", pGraphics, "int", ARGB)
}
UpdateLayeredWindow(hwnd, hdc, x="", y="", w="", h="", Alpha=255){
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	if ((x != "") && (y != ""))
		VarSetCapacity(pt, 8), NumPut(x, pt, 0, "UInt"), NumPut(y, pt, 4, "UInt")
	if (w = "") ||(h = "")
		WinGetPos,,, w, h, ahk_id %hwnd%
	return DllCall("UpdateLayeredWindow", Ptr, hwnd, Ptr, 0, Ptr, ((x = "") && (y = "")) ? 0 : &pt, "int64*", w|h<<32, Ptr, hdc, "int64*", 0, "uint", 0, "UInt*", Alpha<<16|1<<24, "uint", 2)
}
Gdip_BitmapFromScreen(Screen=0, Raster=""){
	if (Screen = 0){
		Sysget, x, 76
		Sysget, y, 77	
		Sysget, w, 78
		Sysget, h, 79
	}
	else if (SubStr(Screen, 1, 5) = "hwnd:")
	{
		Screen := SubStr(Screen, 6)
		if !WinExist( "ahk_id " Screen)
			return -2
		WinGetPos,,, w, h, ahk_id %Screen%
		x := y := 0
		hhdc := GetDCEx(Screen, 3)
	}
	else if (Screen&1 != "")
	{
		Sysget, M, Monitor, %Screen%
		x := MLeft, y := MTop, w := MRight-MLeft, h := MBottom-MTop
	}
	else
	{
		StringSplit, S, Screen, |
		x := S1, y := S2, w := S3, h := S4
	}
	if (x = "") || (y = "") || (w = "") || (h = "")
		return -1
	chdc := CreateCompatibleDC(), hbm := CreateDIBSection(w, h, chdc), obm := SelectObject(chdc, hbm), hhdc := hhdc ? hhdc : GetDC()
	BitBlt(chdc, 0, 0, w, h, hhdc, x, y, Raster)
	ReleaseDC(hhdc)
	pBitmap := Gdip_CreateBitmapFromHBITMAP(hbm)
	SelectObject(chdc, obm), DeleteObject(hbm), DeleteDC(hhdc), DeleteDC(chdc)
	return pBitmap
}
Gdip_SaveBitmapToFile(pBitmap, sOutput, Quality=75){
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	SplitPath, sOutput,,, Extension
	if Extension not in BMP,DIB,RLE,JPG,JPEG,JPE,JFIF,GIF,TIF,TIFF,PNG
		return -1
	Extension := "." Extension
	DllCall("gdiplus\GdipGetImageEncodersSize", "uint*", nCount, "uint*", nSize)
	VarSetCapacity(ci, nSize)
	DllCall("gdiplus\GdipGetImageEncoders", "uint", nCount, "uint", nSize, Ptr, &ci)
	if !(nCount && nSize)
		return -2
	If (A_IsUnicode){
		StrGet_Name := "StrGet"
		Loop, %nCount%
		{
			sString := %StrGet_Name%(NumGet(ci, (idx := (48+7*A_PtrSize)*(A_Index-1))+32+3*A_PtrSize), "UTF-16")
			if !InStr(sString, "*" Extension)
				continue
			
			pCodec := &ci+idx
			break
		}
	} else {
		Loop, %nCount%
		{
			Location := NumGet(ci, 76*(A_Index-1)+44)
			nSize := DllCall("WideCharToMultiByte", "uint", 0, "uint", 0, "uint", Location, "int", -1, "uint", 0, "int",  0, "uint", 0, "uint", 0)
			VarSetCapacity(sString, nSize)
			DllCall("WideCharToMultiByte", "uint", 0, "uint", 0, "uint", Location, "int", -1, "str", sString, "int", nSize, "uint", 0, "uint", 0)
			if !InStr(sString, "*" Extension)
				continue
			
			pCodec := &ci+76*(A_Index-1)
			break
		}
	}
	if !pCodec
		return -3
	if (Quality != 75)
	{
		Quality := (Quality < 0) ? 0 : (Quality > 100) ? 100 : Quality
		if Extension in .JPG,.JPEG,.JPE,.JFIF
		{
			DllCall("gdiplus\GdipGetEncoderParameterListSize", Ptr, pBitmap, Ptr, pCodec, "uint*", nSize)
			VarSetCapacity(EncoderParameters, nSize, 0)
			DllCall("gdiplus\GdipGetEncoderParameterList", Ptr, pBitmap, Ptr, pCodec, "uint", nSize, Ptr, &EncoderParameters)
			Loop, % NumGet(EncoderParameters, "UInt")      ;%
			{
				elem := (24+(A_PtrSize ? A_PtrSize : 4))*(A_Index-1) + 4 + (pad := A_PtrSize = 8 ? 4 : 0)
				if (NumGet(EncoderParameters, elem+16, "UInt") = 1) && (NumGet(EncoderParameters, elem+20, "UInt") = 6)
				{
					p := elem+&EncoderParameters-pad-4
					NumPut(Quality, NumGet(NumPut(4, NumPut(1, p+0)+20, "UInt")), "UInt")
					break
				}
			}      
		}
	}
	if (!A_IsUnicode)
	{
		nSize := DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sOutput, "int", -1, Ptr, 0, "int", 0)
		VarSetCapacity(wOutput, nSize*2)
		DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sOutput, "int", -1, Ptr, &wOutput, "int", nSize)
		VarSetCapacity(wOutput, -1)
		if !VarSetCapacity(wOutput)
			return -4
		E := DllCall("gdiplus\GdipSaveImageToFile", Ptr, pBitmap, Ptr, &wOutput, Ptr, pCodec, "uint", p ? p : 0)
	}
	else
		E := DllCall("gdiplus\GdipSaveImageToFile", Ptr, pBitmap, Ptr, &sOutput, Ptr, pCodec, "uint", p ? p : 0)
	return E ? -5 : 0
}
Gdip_Shutdown(pToken){
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	DllCall("gdiplus\GdiplusShutdown", Ptr, pToken)
	if hModule := DllCall("GetModuleHandle", "str", "gdiplus", Ptr)
		DllCall("FreeLibrary", Ptr, hModule)
	return 0
}
Gdip_CreateBitmapFromHBITMAP(hBitmap, Palette=0){
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	DllCall("gdiplus\GdipCreateBitmapFromHBITMAP", Ptr, hBitmap, Ptr, Palette, A_PtrSize ? "UPtr*" : "uint*", pBitmap)
	return pBitmap
}
SOTE
Posts: 1014
Joined: 15 Jun 2015, 06:21

Re: Help with ImageSearch

30 Oct 2019, 06:38

Tampleeeee wrote:
30 Oct 2019, 05:10
probably it's doesn't work
You should probably describe what you are doing in more detail. The simple script given should work, if what you are doing is to find the same image from Game Logo.png on your screen. We can't read your mind. If that's not what you are doing, or if there is something else involved, you should let us know.
SOTE
Posts: 1014
Joined: 15 Jun 2015, 06:21

Re: Help with ImageSearch

30 Oct 2019, 06:42

boiler wrote:
30 Oct 2019, 05:19
At least one issue is this line. There cannot be spaces around the \ in the file name D: \ Game Logo.png. It needs to be D:\Game Logo.png
Yes that's correct, thanks. Didn't realize those extra spaces were in there.
I'm not seeing how using the Relative option of MouseMove will ever be able to move the cursor to the found image as desired. There are no circumstances where the found coordinates are provided relative to the mouse location, only relative to the screen or the active window.
True. Was thinking of something else. Would be better to simply add a few pixels to get to the desired spot, if needed. The mouse pointer can be at the top left of the image, but it might be preferred that the mouse pointer is center mass of the image. To adjust, in a simple way, example below:

Code: Select all

if ErrorLevel = 0
{
	MsgBox,, Information, The image was found at %FoundX%x%FoundY%`n Wait as will add extra pixels, 3
	FoundX := FoundX + 50
	FoundY := FoundY + 50
	MsgBox,, Information, Mouse pointer will move to %FoundX%x%FoundY%`n Don't touch mouse as app will move it, 3
	MouseMove, %FoundX%,  %FoundY%, 50
}
Last edited by SOTE on 30 Oct 2019, 07:25, edited 3 times in total.
Tampleeeee
Posts: 19
Joined: 12 Oct 2019, 03:25

Re: Help with ImageSearch

30 Oct 2019, 07:05

SOTE wrote:
30 Oct 2019, 06:38
Tampleeeee wrote:
30 Oct 2019, 05:10
probably it's doesn't work
You should probably describe what you are doing in more detail. The simple script given should work, if what you are doing is to find the same image from Game Logo.png on your screen. We can't read your mind. If that's not what you are doing, or if there is something else involved, you should let us know.
Ok, look, I’m launching the first script that I was offered, I open the folder with the given data. I press ^ f and in any case the mouse does not move to the desired file and writes all the time - Could not conduct the search.
User avatar
Bugz000
Posts: 63
Joined: 30 Sep 2013, 10:01

Re: Help with ImageSearch

30 Oct 2019, 07:06

i've always found the inbuilt AHK way to search, find, and click an image clunky unreliable and long winded, i mean it WORKS but its not ideal

consider this https://www.autohotkey.com/boards/viewtopic.php?f=6&t=18719
Image
Image

[14/10/2019][03:06:09] <@tidbit> so, 8 years old. thats way too young for a human centipede.
[29/10/2019][01:48:42] <@tidbit> it should be a cloaca
[6/12/2019][01:53:34] <+Budgii> tidbit: how did you come up with your name?
[6/12/2019][01:53:59] <@tidbit> Budgii: I slapped a goat and phonetically spelt the sound it made
[15/1/2020][17:51:08] <@tidbit> b-b-b-bbaka!
[15/1/2020][17:59:42] <@tidbit> bugz knows his otakunese guys!
[28/2/2020][21:09:36] <@tidbit> find an eagle with some damn self control?
[07/3/2020][00:28:48] <@tidbit> maybe someone was fapping too close to heaven, so god sent a warning
Tampleeeee
Posts: 19
Joined: 12 Oct 2019, 03:25

Re: Help with ImageSearch

30 Oct 2019, 07:14

I do not understand how to use it
SOTE
Posts: 1014
Joined: 15 Jun 2015, 06:21

Re: Help with ImageSearch

30 Oct 2019, 07:18

See other post below.
Last edited by SOTE on 30 Oct 2019, 07:44, edited 1 time in total.
User avatar
Bugz000
Posts: 63
Joined: 30 Sep 2013, 10:01

Re: Help with ImageSearch

30 Oct 2019, 07:19

Tampleeeee wrote:
30 Oct 2019, 07:14
I do not understand how to use it
yeah berban does need to make it a bit simpler at the face of it, here's an example function call pulled from one of my scripts

Code: Select all

FindClick("Button.png", "o95 n am500", X, Y)
this should work in most cases, just plug your own image in and it should be good to go :)

this won't click on the image but it will return the X and Y of the image! if i recall, simply remove the "n" from the flags and it will click it for you too :)

remember to have the findclick library at the bottom of your code! or #included!
Image
Image

[14/10/2019][03:06:09] <@tidbit> so, 8 years old. thats way too young for a human centipede.
[29/10/2019][01:48:42] <@tidbit> it should be a cloaca
[6/12/2019][01:53:34] <+Budgii> tidbit: how did you come up with your name?
[6/12/2019][01:53:59] <@tidbit> Budgii: I slapped a goat and phonetically spelt the sound it made
[15/1/2020][17:51:08] <@tidbit> b-b-b-bbaka!
[15/1/2020][17:59:42] <@tidbit> bugz knows his otakunese guys!
[28/2/2020][21:09:36] <@tidbit> find an eagle with some damn self control?
[07/3/2020][00:28:48] <@tidbit> maybe someone was fapping too close to heaven, so god sent a warning
garry
Posts: 2202
Joined: 22 Dec 2013, 12:50

Re: Help with ImageSearch

30 Oct 2019, 07:29

a basic example for 4 pictures ( in this example 3rd picture not found then continue )

Code: Select all

;- imagesearchxx wgetxx refererxx
;-------- saved at Sonntag, 1. Januar 2017 21:10:02 --------------
;-------- https://autohotkey.com/boards/viewtopic.php?f=5&t=26342&p=123836#p123836 ---
;- example shows 4 pictures
;- maybe add f1a / f3a to 1st and 3th picture name is then wrong so clicks picture-2 AND picture-4
;----------------------------------------------------------------------------
#Warn
#NoEnv
SendMode,Input
SetWorkingDir, %A_ScriptDir%
SetTitleMatchMode,2
SetBatchLines, -1
FileEncoding, UTF-8
Gui,2:default
GUI,2:+AlwaysOnTop
Gui,2: -DPIScale
SS_REALSIZECONTROL := 0x40
Gui,2:Font,s14 ,Lucida console
Gui,2:Color,Black
Gui,2:Color, ControlColor, Black

wa:=A_screenwidth
ha:=A_screenHeight

CoordMode,Pixel,Screen
CoordMode,Mouse,Screen
;-------------------------------------------------------------
i=0
ur1=https://autohotkey.com/boards/images/flags/russia.png
SplitPath,ur1, name1, dir1, ext, name_no_ext, drive
f1 =%a_scriptdir%\%name1%
f1a:=""     ;- wrong name so search next picture

ur2=https://autohotkey.com/boards/images/flags/germany.png
SplitPath,ur2, name2, dir2, ext, name_no_ext, drive
f2 =%a_scriptdir%\%name2%

ur3=https://autohotkey.com/boards/images/flags/china.png
SplitPath,ur3, name3, dir3, ext, name_no_ext, drive
f3 =%a_scriptdir%\%name3%
f3a:=""     ;- wrong name so search next picture

ur4=https://autohotkey.com/boards/images/flags/france.png
SplitPath,ur4, name4, dir4, ext, name_no_ext, drive
f4 =%a_scriptdir%\%name4%


ifnotexist,%f1%
  urldownloadtofile,%ur1%,%f1%
ifnotexist,%f2%
  urldownloadtofile,%ur2%,%f2%
ifnotexist,%f3%
  urldownloadtofile,%ur3%,%f3%
ifnotexist,%f4%
  urldownloadtofile,%ur4%,%f4%

;---------------------------------------------------------------
Gui,2:add,picture, gA1 x10  y10  ,%f1%
Gui,2:add,picture, gA2 x90  y10  ,%f2%
Gui,2:add,picture, gA3 x170 y10  ,%f3%
Gui,2:add,picture, gA4 x250 y10  ,%f4%
gui,2:show, x100 y10 h70 w500 ,F2 to start
return
2guiclose:
exitapp
a1:
msgbox, 262208, ,Clicked Picture %name1%,2
return
a2:
msgbox, 262208, ,Clicked Picture %name2%,2
return
a3:
msgbox, 262208, ,Clicked Picture %name3%,2
return
a4:
msgbox, 262208, ,Clicked Picture %name4%,2
return


f2::
ImageSearch,xf,yf,0,0,500,200,*transblack %f1%    ;- maybe add wrong name for TEST f1a
picx=%name1%
gosub,errorx

ImageSearch,xf,yf,0,0,500,200,*transblack %f2%
picx=%name2%
gosub,errorx

ImageSearch,xf,yf,0,0,%wa%,%ha%,*transblack %f3a%    ;- maybe add wrong name for TEST f3a
picx=%name3%
gosub,errorx

ImageSearch,xf,yf,0,0,%wa%,%ha%,*transblack %f4%
picx=%name4%
gosub,errorx
mousemove,1,1
msgbox, 262208, ,FINISHED
return

errorx:
i++
if errorlevel=1
  {
  msgbox, 262208, ,Error`nTry with next picture in 3 seconds,3
  return
  }
else if errorlevel=2
  {
  msgbox, 262208, ,Picture %picx% not found`nNow continue in 3 seconds with next-picture,3
  return
  }
MouseMove,%xf%,%yf%,
ToolTip,Here is the picture %picx%
sleep,2500
tooltip
MouseClick,left,%xf%,%yf%       ;- click on x-picture
return
;====================== END script =================================



SOTE
Posts: 1014
Joined: 15 Jun 2015, 06:21

Re: Help with ImageSearch  Topic is solved

30 Oct 2019, 07:43

Tampleeeee wrote:
30 Oct 2019, 07:05
SOTE wrote:
30 Oct 2019, 06:38
Tampleeeee wrote:
30 Oct 2019, 05:10
probably it's doesn't work
You should probably describe what you are doing in more detail. The simple script given should work, if what you are doing is to find the same image from Game Logo.png on your screen. We can't read your mind. If that's not what you are doing, or if there is something else involved, you should let us know.
Ok, look, I’m launching the first script that I was offered, I open the folder with the given data. I press ^ f and in any case the mouse does not move to the desired file and writes all the time - Could not conduct the search.
Sorry, didn't see your reply before. Possibly was writing at the same time you were replying.

Looks like you might not be searching for an image on the screen, but want to do a file search for an image? Maybe the interpretation is all mixed up.

ImageSearch is used to find a "copy" of the .png image (.gif or other type) that is on your screen, then your mouse will move to it. Like if I wanted my mouse to move to a picture of an apple on my screen. I would then use Apple.png in the command, ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *12 D:\MyPictures\Apple.png. The command is doing a comparison of what is on the screen to the image file.

Instead, are you trying to search for the file D:\Game Logo.png?

You would then do something like below:
Note- Run will cause whatever application associated with .png on your computer to open, if you have an application that can open it.

Code: Select all

if FileExist("D:\Game Logo.png")
{
	MsgBox, The file exists.
	Run  D:\Game Logo.png
}
if !FileExist("D:\Game Logo.png")
{
	MsgBox, The file does not exist.
}
Last edited by SOTE on 30 Oct 2019, 22:31, edited 1 time in total.
User avatar
boiler
Posts: 4605
Joined: 21 Dec 2014, 02:44

Re: Help with ImageSearch

30 Oct 2019, 09:06

Tampleeeee wrote:
30 Oct 2019, 07:05
Ok, look, I’m launching the first script that I was offered, I open the folder with the given data. I press ^ f and in any case the mouse does not move to the desired file and writes all the time - Could not conduct the search.
If it says "Could not conduct the search", that means the ImageSearch failed because of a problem with the file. Did you remove the extra spaces around the \ in D:\Game Logo.png as was mentioned before? Because if you did not, it would show that exact message.

Return to “Ask For Help”

Who is online

Users browsing this forum: Google [Bot], Rohwedder, submeg and 248 guests