Gdip_PixelSearch Wont Work :(

Get help with using AutoHotkey and its commands and hotkeys
Raymond888
Posts: 17
Joined: 11 Aug 2019, 19:14

Gdip_PixelSearch Wont Work :(

09 Aug 2020, 01:51

I been messing about with pixel search and getpixel with gdip, I can get Gdip_getpixel to work, but absolutely nothing I do will get Gdip_pixelsearch to work and there hardly any other threads out there explaining how it works with examples :(
Please please AHK gods help me out.

I tried to type of scripts, one an copy/pasta from the original gdip_pixelsearch thread on the forums and it doesn't even work. I assume it might have something to do with the colour formatting maybe?

Code: Select all

#Include Gdip_All.ahk
#Include Gdip_PixelSearch.ahk

F1::

pToken := Gdip_Startup()

vPosX := 0, vPosY := 0
vPosW := 1920, vPosH := 1080
vImgPos := Format("{}|{}|{}|{}", vPosX, vPosY, vPosW, vPosH)
pBitmap := Gdip_BitmapFromScreen(vImgPos)

if !Gdip_PixelSearch(pBitmap, ARGB := 0x4294967295, x, y)
	MsgBox, Pixel %ARGB% found at (%x%, %y%)
else
	MsgBox, Pixel %ARGB% not found
Gdip_DisposeImage(pBitmap)
Gdip_Shutdown(pToken)

return
and the source code from the ahk archive thread is;

Code: Select all

#Include Gdip_All.ahk

F1::

pToken := Gdip_Startup()
pBitmap := Gdip_CreateBitmapFromFile("image.png")
if !Gdip_PixelSearch(pBitmap, ARGB := 0xff0118d9, x, y)
	MsgBox, Pixel %ARGB% found at (%x%, %y%)
else
	MsgBox, Pixel %ARGB% not found
Gdip_DisposeImage(pBitmap)
Gdip_Shutdown(pToken)
return

;#####################################################################################

Gdip_PixelSearch(pBitmap, ARGB, ByRef x, ByRef y)
{
	static _PixelSearch
	if !_PixelSearch
	{
		MCode_PixelSearch := "8B44241099535583E2035603C233F6C1F80239742418577E388B7C24148B6C24248B5424188D1C85000000008D64240033C085"
		. "D27E108BCF3929743183C00183C1043BC27CF283C60103FB3B74241C7CDF8B4424288B4C242C5F5EC700FFFFFFFF5DC701FFFFFFFF83C8FF5BC38B4C2"
		. "4288B54242C5F890189325E5D33C05BC3"

		VarSetCapacity(_PixelSearch, StrLen(MCode_PixelSearch)//2)
		Loop % StrLen(MCode_PixelSearch)//2      ;%
			NumPut("0x" SubStr(MCode_PixelSearch, (2*A_Index)-1, 2), _PixelSearch, A_Index-1, "char")
	}
	Gdip_GetImageDimensions(pBitmap, Width, Height)
	if !(Width && Height)
		return -1

	if (E1 := Gdip_LockBits(pBitmap, 0, 0, Width, Height, Stride1, Scan01, BitmapData1))
		return -2

	x := y := 0
	E := DllCall(&_PixelSearch, "uint", Scan01, "int", Width, "int", Height, "int", Stride1, "uint", ARGB, "int*", x, "int*", y)
	Gdip_UnlockBits(pBitmap, BitmapData1)
	return (E = "") ? -3 : E
}
User avatar
boiler
Posts: 5909
Joined: 21 Dec 2014, 02:44

Re: Gdip_PixelSearch Wont Work :(

09 Aug 2020, 04:15

Apparently, you tried to use the decimal value for pure white, but you preceded it with 0x, which indicates the number is in hex format. Either just put the decimal number by itself (4294967295) or use the hex representation (0xFFFFFFFF).
Raymond888
Posts: 17
Joined: 11 Aug 2019, 19:14

Re: Gdip_PixelSearch Wont Work :(

09 Aug 2020, 14:47

Thanks for your reply boiler, but even if you copy tic example direct it does not work, I have searched every single thread about this, and tried for hours, but I can't get it to work and with DDL calls you can't really work backwards :(
https://autohotkey.com/board/topic/79077-gdip-pixelsearch/ is Tic example, but just doesn't work. Is it a bug? surely it can't be me <LOL>

Code: Select all

#Include Gdip_all.ahk

F1::

pToken := Gdip_Startup()
pBitmap := Gdip_CreateBitmapFromFile("image.png")
if !Gdip_PixelSearch(pBitmap, ARGB := 0xFFFFFFFF, x, y)
	MsgBox, Pixel %ARGB% found at (%x%, %y%)
else
	MsgBox, Pixel %ARGB% not found
Gdip_DisposeImage(pBitmap)
Gdip_Shutdown(pToken)
return

;#####################################################################################

Gdip_PixelSearch(pBitmap, ARGB, ByRef x, ByRef y)
{
	static _PixelSearch
	if !_PixelSearch
	{
		MCode_PixelSearch := "8B44241099535583E2035603C233F6C1F80239742418577E388B7C24148B6C24248B5424188D1C85000000008D64240033C085"
		. "D27E108BCF3929743183C00183C1043BC27CF283C60103FB3B74241C7CDF8B4424288B4C242C5F5EC700FFFFFFFF5DC701FFFFFFFF83C8FF5BC38B4C2"
		. "4288B54242C5F890189325E5D33C05BC3"

		VarSetCapacity(_PixelSearch, StrLen(MCode_PixelSearch)//2)
		Loop % StrLen(MCode_PixelSearch)//2      ;%
			NumPut("0x" SubStr(MCode_PixelSearch, (2*A_Index)-1, 2), _PixelSearch, A_Index-1, "char")
	}
	Gdip_GetImageDimensions(pBitmap, Width, Height)
	if !(Width && Height)
		return -1

	if (E1 := Gdip_LockBits(pBitmap, 0, 0, Width, Height, Stride1, Scan01, BitmapData1))
		return -2

	x := y := 0
	E := DllCall(&_PixelSearch, "uint", Scan01, "int", Width, "int", Height, "int", Stride1, "uint", ARGB, "int*", x, "int*", y)
	Gdip_UnlockBits(pBitmap, BitmapData1)
	return (E = "") ? -3 : E
}
User avatar
boiler
Posts: 5909
Joined: 21 Dec 2014, 02:44

Re: Gdip_PixelSearch Wont Work :(

09 Aug 2020, 15:11

That code only works with 32-bit AHK. You’re probably running a 64-bit version.
Raymond888
Posts: 17
Joined: 11 Aug 2019, 19:14

Re: Gdip_PixelSearch Wont Work :(

09 Aug 2020, 15:33

Wasn't sure what version I was, so I uninstalled and put the 32 bit on and still no luck :(
User avatar
boiler
Posts: 5909
Joined: 21 Dec 2014, 02:44

Re: Gdip_PixelSearch Wont Work :(

09 Aug 2020, 15:57

Are you sure your image has pure white pixels in it?

By the way, you don’t have to uninstall and reinstall AHK. You can just drag and drop the script file onto the binary file that you want to run it with.
Raymond888
Posts: 17
Joined: 11 Aug 2019, 19:14

Gdip_PixelSeach Working Now!

09 Aug 2020, 16:41

boiler, omg omg omg I got it to work ;) ( :bravo: :dance: :xmas: :clap: :lol: )

I don't know what did the trick, ever reinstalling AHK in 32 bits or changing the GDI Library from the old Tic version to the new MasterFocus version;
I was trying all the colours of the rainbow, but focused on pure white because its so everywhere ;best grammer ever.

Anyway the final code I got to work which searches the whole screen and and reports back is below, also its easily changeable. Its a pity the GDI world doesn't have the lovely setup examples and guides like the standard AHK library which is so well made, congrats to the team for that.

Just some background, I had this all working with the standard pixel search and get pixel, What I was doing was trying to avoid false positives with the pixelsearch, so I created a programme where its finds a pixel, then searches all the pixel around it which are all previouslly known and saved as variables already, 5 up, 5 left, 5 down, 5 right, and all in between, so 11 x 11 = 131 minus the original "focus dot" which was originally found. And using loops and increasing the variation which gives a results dependant on variation level it was found owas converted to a likelyhood of correctness value, which then can be used to give a value of match likelyhood when all put together, which I believe is far more likely to report good matches, however speed was a major problem as it was doing far far more then standard pixel search. Particually when doing the variation searches for each pixel * 130 which increased each time (upto 5) = 550 possible searches, normally gdi would be enough to speed this up I would assume... But cause you cant do variation with Gdi_PixelSearch so I'm going to use the Gdi_GetPixel and convert each colour to RGB 225 and then assign matching values on how close they are. Also the problem with pixelsearch was when getting all the "focused dots" before running the secondary test was that if for example you do "pixelsearch, outputx, outputy, x1, y1, x500, y500, colour, variation, fast" and you want to loop all matches. So for example you find a "focusdot" on x50, y30. Well then you make it "if errorlevel = 0" re-enter the loop, but, its hard to start the search from x51, y31,to x500, y500 because it then it will miss the x1-x50 for all rows below y31. So ever you have to just go backto x1, y32 and not finish that row 31. I guess just writting this I had the idea you could do something to finish search the row 31 with x51, y31, x500, y31 then start fresh on the next row using variables and all that maths stuff :) However do you or anyone know of any other ways this could be done more easily?

Anyway here is the working code, thanks so much for your help :)

Code: Select all

SetWorkingDir %A_ScriptDir%
#Include Gdip_All.ahk
#Include Gdip_PixelSearch.ahk

F1::

pToken := Gdip_Startup()

vPosX := 0, vPosY := 0
vPosW := 1920, vPosH := 1080
vImgPos := Format("{}|{}|{}|{}", vPosX, vPosY, vPosW, vPosH)
pBitmap := Gdip_BitmapFromScreen(vImgPos)
ARGB := 0xFFFFFFFF

if !Gdip_PixelSearch(pBitmap, ARGB, x, y)
	MsgBox, Pixel %ARGB% found at (%x%, %y%) - %Errorlevel%
else
	MsgBox, Pixel %ARGB% not found - %Errorlevel%
Gdip_DisposeImage(pBitmap)
Gdip_Shutdown(pToken)

return
User avatar
boiler
Posts: 5909
Joined: 21 Dec 2014, 02:44

Re: Gdip_PixelSearch Wont Work :(

09 Aug 2020, 16:51

I would re-write Gdip_PixelSearch to allow variation.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Lukunka, mabohango, mikeyww, PlumpSamurai and 54 guests