Search Image & Show it elsewhere

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
nidark
Posts: 11
Joined: 29 Oct 2020, 10:42

Search Image & Show it elsewhere

29 Oct 2020, 10:50

Hello,
I am trying to:
1. Search for an image <- Should be able with ImageSearch
2. I want to clip a bigger part of the screen containing the image I searched for <- I should be able to use the ImageSearch coords and extrapolate the coords for the bigger clip
3. "Take" that (bigger) image and show it on the screen in another location.

The question is how do I "copy" the image from the screen and show it in another place ?

Thank you!
nidark
Posts: 11
Joined: 29 Oct 2020, 10:42

Re: Search Image & Show it elsewhere

29 Oct 2020, 11:07

Thanks! I will try it out!
nidark
Posts: 11
Joined: 29 Oct 2020, 10:42

Re: Search Image & Show it elsewhere

01 Nov 2020, 14:23

I used that example (works fine) and tried to show the snap into a GUI (my target being to see that on the screen in another place)

I used the standard GUI .. adding the snap as bitmnap

Code: Select all

Gui, Add, Pic, w300 h-1, snap
Gui, Show
Unfortunately the bitmap was not visible in GUI ... I guess I a missing something ...


Full code ...

Code: Select all

#include lib\Gdip_All.ahk ; Replace with your GDI path
out = test.png ; Replace as needed
CoordMode, Mouse

F3:: ; Hold F3 to make a selection, then release
MouseGetPos, x1, y1
SoundBeep, 1500, 20
KeyWait, %A_ThisHotkey%
SoundBeep, 1000, 20
MouseGetPos, x2, y2
width := x2 - x1, height := y2 - y1
If (width < 4 && height < 4)
 Return
pToken := Gdip_Startup(), 
snap := Gdip_BitmapFromScreen(x1 "|" y1 "|" width "|" height)
Gdip_SetBitmapToClipboard(snap), 
Gdip_SaveBitmapToFile(snap, out)

Gui, Add, Pic, w300 h-1, snap
Gui, Show

Gdip_DisposeImage(snap), 
Gdip_Shutdown(pToken)
Return
nidark
Posts: 11
Joined: 29 Oct 2020, 10:42

Re: Search Image & Show it elsewhere

04 Nov 2020, 06:38

Does anyone have a hint on the reason why the bitmap is not showing on GUI ?
User avatar
mikeyww
Posts: 27246
Joined: 09 Sep 2014, 18:38

Re: Search Image & Show it elsewhere

04 Nov 2020, 07:44

In your Add, Pic, change snap to %out%, which is the path to the image file.
nidark
Posts: 11
Joined: 29 Oct 2020, 10:42

Re: Search Image & Show it elsewhere

04 Nov 2020, 12:46

Thanks - works!

PS: I was hoping I dont need to save to a file, but this will work as well!
User avatar
mikeyww
Posts: 27246
Joined: 09 Sep 2014, 18:38

Re: Search Image & Show it elsewhere

04 Nov 2020, 16:09

The last parameter is the filename of the image, which is assumed to be in A_WorkingDir if an absolute path isn't specified.
It could be a temporary file that is deleted later, of course.
nidark
Posts: 11
Joined: 29 Oct 2020, 10:42

Re: Search Image & Show it elsewhere

05 Nov 2020, 12:20

Yes of course ...
Actualy I will do this every 200-500 ms so I hope will work ok with files :)

PS: I am trying to show a "status" in a better postion on the screen and as I cant change the original layout I am practicaly tryin to duplicate and refresh constantly ...
User avatar
mikeyww
Posts: 27246
Joined: 09 Sep 2014, 18:38

Re: Search Image & Show it elsewhere

05 Nov 2020, 12:29

Below is an example without files.

Code: Select all

#include lib\Gdip_All.ahk ; Replace with your GDI path
CoordMode, Mouse

F3:: ; Hold F3 to make a selection, then release
MouseGetPos, x1, y1
SoundBeep, 1500, 20
KeyWait, %A_ThisHotkey%
SoundBeep, 1000, 20
MouseGetPos, x2, y2
width := x2 - x1, height := y2 - y1
If (width < 4 && height < 4)
 Return
pToken := Gdip_Startup()
snap := Gdip_BitmapFromScreen(x1 "|" y1 "|" width "|" height)
Gui, New
; https://www.autohotkey.com/boards/viewtopic.php?p=117639#p117639
Gui, Add, Pic,, % "HBITMAP:*" Gdip_CreateHBITMAPFromBitmap(snap)
Gui, Show
Gdip_DisposeImage(snap), Gdip_Shutdown(pToken)
Return
nidark
Posts: 11
Joined: 29 Oct 2020, 10:42

Re: Search Image & Show it elsewhere

06 Nov 2020, 13:01

Oh, nice!
Thanks a lot for taking the time!
nidark
Posts: 11
Joined: 29 Oct 2020, 10:42

Re: Search Image & Show it elsewhere

08 Nov 2020, 06:51

Hi,
I advanced pretty decent, but I do have a problem.
I am trying to update the bitmap every 250 miliseconds, however it does not refresh.
I assume there is something I am missing to refresh/redraw the GUI.

MainAgro is started by a Key and shows the bitmap from a location to the middle of the screen.
UpdateGui shows correctly the first bitmap but does not update after ...

Code: Select all

MainAgro(){
CustomColor := "EEAA99" 
Gui +LastFound +AlwaysOnTop -Caption +ToolWindow
Gui, Color, %CustomColor%
WinSet, TransColor, %CustomColor% 200

SetTimer, UpdateGui, 250
Gosub, UpdateGui

Gui, Show, NoActivate
Return
}


UpdateGui:
If (width < 4 && height < 4)
Return

pToken := Gdip_Startup()
snap := Gdip_BitmapFromScreen(AgroBarMainX1  "|" AgroBarMainY1 "|" width "|" height)
Gui, Add, Pic,Y++250 w75 h-1, % "HBITMAP:*" Gdip_CreateHBITMAPFromBitmap(snap)
Gdip_DisposeImage(snap), Gdip_Shutdown(pToken)

return
User avatar
mikeyww
Posts: 27246
Joined: 09 Sep 2014, 18:38

Re: Search Image & Show it elsewhere

08 Nov 2020, 07:49

GuiControl can be used to change the contents of a GUI control.

Code: Select all

#include q:\vis2\lib\Gdip_All.ahk
Global snap
pToken := Gdip_Startup()
updateGUI(50, 50, 200, 100)
Sleep, 1000
updateGUI(150, 150, 200, 100)
Return

updateGUI(x, y, width, height){
 Static pic, started := False
 snap := Gdip_BitmapFromScreen(x "|" y "|" width "|" height)
 hbitmap := Gdip_CreateHBITMAPFromBitmap(snap), image := "HBITMAP:*" hbitmap
 If started
  GuiControl,, pic, %image%
 Else
  Gui, Add, Picture, w200 h100 vpic, %image%
 started := True
 Gui, Show
}

F4::
Gdip_DisposeImage(snap), Gdip_Shutdown(pToken)
ExitApp
nidark
Posts: 11
Joined: 29 Oct 2020, 10:42

Re: Search Image & Show it elsewhere

08 Nov 2020, 14:44

Works like a charm!
Thank you!

Code: Select all

MainAgro(){
	CustomColor := "EEAA99" 
	Gui +LastFound +AlwaysOnTop -Caption +ToolWindow
	Gui, Color, %CustomColor%
	WinSet, TransColor, %CustomColor% 200
	SetTimer, UpdateOSD, 250
	updateGUI(AgroBarMainX1,AgroBarMainY1, gwidth, gheight)
	Return
}

UpdateOSD:
	result := updateGUI(AgroBarMainX1,AgroBarMainY1, gwidth, gheight)
	return

updateGUI(x, y, width, height){
	Static pic, started := False
	snap := Gdip_BitmapFromScreen(x "|" y "|" width "|" height)
	hbitmap := Gdip_CreateHBITMAPFromBitmap(snap), image := "HBITMAP:*" hbitmap
	If started
		GuiControl,, pic, %image%
	Else
		Gui, Add, Picture, Y++250 w75 h-1 vpic, %image%
		started := True
		Gui, Show
	return
}
User avatar
mikeyww
Posts: 27246
Joined: 09 Sep 2014, 18:38

Re: Search Image & Show it elsewhere

08 Nov 2020, 15:59

No problem.

You have an unintended effect in your update function: the following lines are always executed.

Code: Select all

started := True
Gui, Show
That is because in AHK, braces, rather than indentation, define a code block. Your Else "block" contains no braces, so the only line attached to Else is the same line or, if that line contains nothing else, the line that follows it.

It turns out, in your particular script here, that it doesn't matter if those two lines are executed with each call to the function. You actually don't need either of them to execute after the first run, but they serve no purpose in those subsequent runs.

A couple of other little tweaks in case you are interested in fine tuning:

1. CustomColor := "EEAA99" is OK but can also be preceded by Static. This prevents the need to redefine that variable during each function call. You won't see any difference directly in your script; it's more of a technicality. In some scripts, it may matter.

2. Return without returning anything-- as a function's last command-- has no effect and can be deleted. The function will still return upon the closing brace. In both instances, nothing will be returned, so Return has no effect.
nidark
Posts: 11
Joined: 29 Oct 2020, 10:42

Re: Search Image & Show it elsewhere

09 Nov 2020, 09:05

Nice points - thank you.
On Return I've seen it used in a few examples using this without a return value ... I was wondering about its utility, but I guess others are picking this up and using it ... like I did :D

.......
I was happy that I finished but ... it seems that when the timmer is active the active window does not get any input from my keyboard.
I think is because I keep the CPU busy ... and gave a few Sleep between the commnds, so seems ok now ..

Code: Select all

MainAgro(){
	static CustomColor := "EEAA99" 
	Gui +LastFound +AlwaysOnTop -Caption +ToolWindow
	Gui, Color, %CustomColor%
	WinSet, TransColor, %CustomColor% 200
	SetTimer, UpdateOSD, 250
	updateGUI(AgroBarMainX1,AgroBarMainY1, gwidth, gheight)	
}

UpdateOSD:
	result := updateGUI(AgroBarMainX1,AgroBarMainY1, gwidth, gheight)
	RandSleep(50,60) 	

updateGUI(x, y, width, height){
	Static pic, started := False
	snap := Gdip_BitmapFromScreen(x "|" y "|" width "|" height)
	hbitmap := Gdip_CreateHBITMAPFromBitmap(snap), image := "HBITMAP:*" hbitmap
	If started
		GuiControl,, pic, %image%
	Else {
		Gui, Add, Picture, Y++250 w75 h-1 vpic, %image%
		started := True
		Gui, Show
		}	
}

RandSleep(x,y) {
Random, rand, %x%, %y%
Sleep %rand%
}
User avatar
mikeyww
Posts: 27246
Joined: 09 Sep 2014, 18:38

Re: Search Image & Show it elsewhere

09 Nov 2020, 09:26

Since you removed Return from UpdateOSD, you might get some unexpected effects. Nonetheless, these rapid-cycle "magnifier" kinds of programs tend to require a lot of PC resources, as you are repeatedly capturing and processing a large amount of data.

I did not mean that you can remove Return from a subroutine. You can remove it from a function if it is the last command and is not returning anything. Some people still leave it there for consistency or readability.
nidark
Posts: 11
Joined: 29 Oct 2020, 10:42

Re: Search Image & Show it elsewhere

09 Nov 2020, 09:56

Makes sense. Thank you!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 228 guests