Gdip_PixelSearch Wont Work :(

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Raymond888
Posts: 17
Joined: 11 Aug 2019, 19:14

Gdip_PixelSearch Wont Work :(

Post by Raymond888 » 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: 16767
Joined: 21 Dec 2014, 02:44

Re: Gdip_PixelSearch Wont Work :(

Post by boiler » 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 :(

Post by Raymond888 » 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: 16767
Joined: 21 Dec 2014, 02:44

Re: Gdip_PixelSearch Wont Work :(

Post by boiler » 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 :(

Post by Raymond888 » 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: 16767
Joined: 21 Dec 2014, 02:44

Re: Gdip_PixelSearch Wont Work :(

Post by boiler » 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!

Post by Raymond888 » 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: 16767
Joined: 21 Dec 2014, 02:44

Re: Gdip_PixelSearch Wont Work :(

Post by boiler » 09 Aug 2020, 16:51

I would re-write Gdip_PixelSearch to allow variation.

User avatar
ibieel
Posts: 216
Joined: 17 Oct 2021, 23:30

Re: Gdip_PixelSearch Wont Work :(

Post by ibieel » 19 Oct 2021, 02:20

boiler wrote:
09 Aug 2020, 16:51
I would re-write Gdip_PixelSearch to allow variation.
Could you give a tip of how can I do this?
I'm playing with GDIP now, I do not understand anything about the source code of this library.
how to put on gdip_pixelsearch a pixel variation (* n)?

sooyke_
Posts: 25
Joined: 18 Nov 2020, 10:27

Re: Gdip_PixelSearch Wont Work :(

Post by sooyke_ » 19 Oct 2021, 04:29

You can use imagesearch that has more options and a needle bitmap of 1 pixel.

example ( only 32bit ahk ! )

Code: Select all

#include *i gdip_all.ahk

If !pToken := Gdip_Startup()
{
	MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
	ExitApp
}

;---------------- create demo gui with pixel color =0xff28f028---------------- 
hbitmap:=Create_bgrd_png()


Gui, add,text,+0xE w200 h200 hwndimage_hwnd       ; +0xE is SS_BITMAP
Gui, show,,demo

SendMessage, (STM_SETIMAGE:=0x172), (IMAGE_BITMAP:=0x0), hBitmap,, ahk_id %image_hwnd%
win_title:="demo"
;---------------- end create demo gui ---------------- 
sleep 2000

mycolor:=0xff28f028  ; create needle with pixel_color 

pBitmap_needle :=Gdip_CreateBitmap(1,1)
pGraphics:=Gdip_GraphicsFromImage(pBitmap_needle)
Gdip_GraphicsClear(pGraphics, mycolor)


pBitmap := Gdip_BitmapFromHWND(WinExist(win_title))


variation:=20

/*

Gdip_ImageSearch(pBitmapHaystack,pBitmapNeedle,ByRef OutputList=""
,OuterX1=0,OuterY1=0,OuterX2=0,OuterY2=0,Variation=0,Trans=""
,SearchDirection=1,Instances=1,LineDelim="`n",CoordDelim=",")

*/

Gdip_ImageSearch(pBitmap,pBitmap_Needle,out_list,30,30,180,180,variation)
pos:=StrSplit(out_list,",")
MouseMove,% pos[1] ,% pos[2]
sleep 1000
tooltip % pos[1] "    " pos[2]
return


esc::

Gdip_DisposeImage(pBitmap)
Gdip_DisposeImage(pBitmap_needle)
Gdip_Shutdown(pToken)

exitapp


;---------------- ---------------- ---------------- ---------------- ---------------- 
; only for creating demo gui image

; ##################################################################################
; # This #Include file was generated by Image2Include.ahk, you must not change it! #
; ##################################################################################
Create_bgrd_png(NewHandle := False) {
Static hBitmap := 0
If (NewHandle)
   hBitmap := 0
If (hBitmap)
   Return hBitmap
VarSetCapacity(B64, 396 << !!A_IsUnicode)
B64 := "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAIAAAAiOjnJAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4AkZBiEH7Oc3tAAAAB10RVh0Q29tbWVudABDcmVhdGVkIHdpdGggVGhlIEdJTVDvZCVuAAAAn0lEQVR42u3QQQ0AIAwEMELOxBzMvzIk8J4FaCV0LQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAC+kEoqHh62FQAAAAAAAAAAAAAAwNSnJQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMB0AUZwAYVNlk1sAAAAAElFTkSuQmCC"
If !DllCall("Crypt32.dll\CryptStringToBinary", "Ptr", &B64, "UInt", 0, "UInt", 0x01, "Ptr", 0, "UIntP", DecLen, "Ptr", 0, "Ptr", 0)
   Return False
VarSetCapacity(Dec, DecLen, 0)
If !DllCall("Crypt32.dll\CryptStringToBinary", "Ptr", &B64, "UInt", 0, "UInt", 0x01, "Ptr", &Dec, "UIntP", DecLen, "Ptr", 0, "Ptr", 0)
   Return False
; Bitmap creation adopted from "How to convert Image data (JPEG/PNG/GIF) to hBITMAP?" by SKAN
; -> http://www.autohotkey.com/board/topic/21213-how-to-convert-image-data-jpegpnggif-to-hbitmap/?p=139257
hData := DllCall("Kernel32.dll\GlobalAlloc", "UInt", 2, "UPtr", DecLen, "UPtr")
pData := DllCall("Kernel32.dll\GlobalLock", "Ptr", hData, "UPtr")
DllCall("Kernel32.dll\RtlMoveMemory", "Ptr", pData, "Ptr", &Dec, "UPtr", DecLen)
DllCall("Kernel32.dll\GlobalUnlock", "Ptr", hData)
DllCall("Ole32.dll\CreateStreamOnHGlobal", "Ptr", hData, "Int", True, "PtrP", pStream)
hGdip := DllCall("Kernel32.dll\LoadLibrary", "Str", "Gdiplus.dll", "UPtr")
VarSetCapacity(SI, 16, 0), NumPut(1, SI, 0, "UChar")
DllCall("Gdiplus.dll\GdiplusStartup", "PtrP", pToken, "Ptr", &SI, "Ptr", 0)
DllCall("Gdiplus.dll\GdipCreateBitmapFromStream",  "Ptr", pStream, "PtrP", pBitmap)
DllCall("Gdiplus.dll\GdipCreateHBITMAPFromBitmap", "Ptr", pBitmap, "PtrP", hBitmap, "UInt", 0)
DllCall("Gdiplus.dll\GdipDisposeImage", "Ptr", pBitmap)
DllCall("Gdiplus.dll\GdiplusShutdown", "Ptr", pToken)
DllCall("Kernel32.dll\FreeLibrary", "Ptr", hGdip)
DllCall(NumGet(NumGet(pStream + 0, 0, "UPtr") + (A_PtrSize * 2), 0, "UPtr"), "Ptr", pStream)
Return hBitmap
}

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

Re: Gdip_PixelSearch Wont Work :(

Post by boiler » 19 Oct 2021, 06:48

ibieel wrote:
19 Oct 2021, 02:20
boiler wrote:
09 Aug 2020, 16:51
I would re-write Gdip_PixelSearch to allow variation.
Could you give a tip of how can I do this?
I'm playing with GDIP now, I do not understand anything about the source code of this library.
how to put on gdip_pixelsearch a pixel variation (* n)?
You would have to add a parameter to the Gdip_PixelSearch function for the variation, then you would change the C code where it searches for a matching color value of the pixel to instead check to see if each RGB component is within the allowable range, which would be that variation parameter and be passed to the C function as a parameter. Then you would need to compile the C code into machine code (MCode) and replace the MCode that is there.

User avatar
ibieel
Posts: 216
Joined: 17 Oct 2021, 23:30

Re: Gdip_PixelSearch Wont Work :(

Post by ibieel » 20 Oct 2021, 00:13

boiler wrote:
19 Oct 2021, 06:48
ibieel wrote:
19 Oct 2021, 02:20
boiler wrote:
09 Aug 2020, 16:51
I would re-write Gdip_PixelSearch to allow variation.
Could you give a tip of how can I do this?
I'm playing with GDIP now, I do not understand anything about the source code of this library.
how to put on gdip_pixelsearch a pixel variation (* n)?
You would have to add a parameter to the Gdip_PixelSearch function for the variation, then you would change the C code where it searches for a matching color value of the pixel to instead check to see if each RGB component is within the allowable range, which would be that variation parameter and be passed to the C function as a parameter. Then you would need to compile the C code into machine code (MCode) and replace the MCode that is there.
wow, its very hard :crazy:
someone in order to receive some bitcoins for this work?

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

Re: Gdip_PixelSearch Wont Work :(

Post by boiler » 20 Oct 2021, 04:11

I would do it for free, but I don’t have time right mow. Maybe in a couple weeks.

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Gdip_PixelSearch Wont Work :(

Post by malcev » 20 Oct 2021, 07:11

You can search without machine code.

Code: Select all

SearchPixel := 0xFF336EA5   ; argb
variation := 30

SetBatchLines, -1
pToken := Gdip_Startup()
pBitmap := Gdip_BitmapFromScreen()
width := Gdip_GetImageWidth(pBitmap)
height := Gdip_GetImageHeight(pBitmap)
Gdip_LockBits(pBitmap, 0, 0, width, height, Stride, Scan0, BitmapData)
loop % height
{
   A_IndexY := A_Index-1
   loop % width
   {
      CurrentPixel := NumGet(Scan0+0, (A_Index-1)*4 + A_IndexY*Stride, "uint")
      if (abs((SearchPixel & 0xff) - (CurrentPixel & 0xff)) <= variation) and (abs(((SearchPixel >> 8) & 0xff) - ((CurrentPixel >> 8) & 0xff)) <= variation) and (abs(((SearchPixel >> 16) & 0xff) - ((CurrentPixel >> 16) & 0xff)) <= variation)
         msgbox % A_Index-1 " " A_IndexY
   }
}
Gdip_UnlockBits(pBitmap, BitmapData)
Gdip_DisposeImage(pBitmap)
Gdip_Shutdown(pToken)

phoenixL337
Posts: 12
Joined: 17 Oct 2021, 04:30

Re: Gdip_PixelSearch Wont Work :(

Post by phoenixL337 » 20 Oct 2021, 11:14

Code: Select all

PixelSearch, FoundX, FoundY, 798, 392, 1539, 845, 0xB4A55E, 3, Fast RGB
If (ErrorLevel = 0)
{
SetKeyDelay, 0, 50
sleep, 500
ControlSend, , t, ahk_exe javaw.exe
Sleep, 30
ControlSend, , /, ahk_exe javaw.exe
Sleep, 30
ControlSend, , s, ahk_exe javaw.exe
Sleep, 30
ControlSend, , k, ahk_exe javaw.exe
Sleep, 30
ControlSend, , y, ahk_exe javaw.exe
Sleep, 30
ControlSend, , b, ahk_exe javaw.exe
Sleep, 30
ControlSend, , l, ahk_exe javaw.exe
Sleep, 30
ControlSend, , o, ahk_exe javaw.exe
Sleep, 30
ControlSend, , c, ahk_exe javaw.exe
sleep, 30
ControlSend, , k, ahk_exe javaw.exe
sleep, 30
ControlSend, , {enter}, ahk_exe javaw.exe
Sleep, 5000
ControlSend, , t, ahk_exe javaw.exe
Sleep, 30
ControlSend, , /, ahk_exe javaw.exe
Sleep, 30
ControlSend, , i, ahk_exe javaw.exe
Sleep, 30
ControlSend, , s, ahk_exe javaw.exe
sleep, 30
ControlSend, , {enter}, ahk_exe javaw.exe
sleep, 5000
reload
hi i need help to make this code run in the background
its for a game i play
i want it to search for a pixel while i am doing other things
Thanks for help

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Gdip_PixelSearch Wont Work :(

Post by malcev » 20 Oct 2021, 12:43

You need to learn how works gdi+.

phoenixL337
Posts: 12
Joined: 17 Oct 2021, 04:30

Re: Gdip_PixelSearch Wont Work :(

Post by phoenixL337 » 20 Oct 2021, 13:19

malcev wrote:
20 Oct 2021, 12:43
You need to learn how works gdi+.
help plz

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Gdip_PixelSearch Wont Work :(

Post by malcev » 20 Oct 2021, 13:55

No. Read docs.
By the way searching can be speed up to 25% like this:

Code: Select all

SearchPixel := 0xFF336EA5   ; argb
variation := 30

SetBatchLines, -1
pToken := Gdip_Startup()
loop 3
{
   SearchPixelArray%A_Index% := []
   start := ((SearchPixel >> (A_Index-1)*8) & 0xff)-variation
   end := ((SearchPixel >> (A_Index-1)*8) & 0xff)+variation
   if (start < 0)
      start := 0
   if (end > 255)
      end := 255
   index := A_Index
   loop % end-start+1
      SearchPixelArray%index%[start+A_Index-1] := 1
}

pBitmap := Gdip_BitmapFromScreen()
width := Gdip_GetImageWidth(pBitmap)
height := Gdip_GetImageHeight(pBitmap)
Gdip_LockBits(pBitmap, 0, 0, width, height, Stride, Scan0, BitmapData)
loop % height
{
   A_IndexY := A_Index-1
   loop % width
   {
      CurrentPixel := NumGet(Scan0+0, (A_Index-1)*4 + A_IndexY*Stride, "uint")
      if SearchPixelArray1[CurrentPixel & 0xff] and SearchPixelArray2[(CurrentPixel >> 8) & 0xff] and SearchPixelArray3[(CurrentPixel >> 16) & 0xff]
         msgbox % A_Index-1 " " A_IndexY
   }
}
Gdip_UnlockBits(pBitmap, BitmapData)
Gdip_DisposeImage(pBitmap)

Gdip_Shutdown(pToken)

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Gdip_PixelSearch Wont Work :(

Post by malcev » 20 Oct 2021, 21:58

Searching using machine code

Code: Select all

setbatchlines -1
pToken := Gdip_Startup()
pBitmap := Gdip_BitmapFromScreen()
if !Gdip_PixelSearch(pBitmap, ARGB := 0xFF336EA5, x, y, 30)
   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, variation)
{
	static MCode_PixelSearch
	if !MCode_PixelSearch
           MCode_PixelSearch := MCode("2,x86:VVdWU4PsEItMJDCLRCQ0i3QkOIXJjVEDD0nRi0wkLMH6AoXJD46RAAAAicEPtvzHRCQIAAAAAA+26MH5EIk8JA+2+Yl8JASNPJUAAAAAiXwkDIt8JCSLRCQoMcmFwH5IixSPD7bCKeiJw8H7HzHYKdg58H8qD7bGKwQkicPB+x8x2CnYOfB/F8HqEA+20itUJASJ0MH4HzHCKcI58n5Bg8EBOUwkKHW4g0QkCAEDfCQMi0QkCDlEJCx1m4tEJDzHAP////+LRCRAxwD/////g8QQuP////9bXl9dw410JgCLRCQ8i3wkCIkIi0QkQIk4g8QQMcBbXl9dww==,x64:QVdBVkFVQVRVV1ZTSIPsGESLlCSIAAAARYXJQY1BA0GJ1UmJzEQPSMiLlCSAAAAARYnGQcH5AkSJTCQMRYXAD46KAAAAidcPtvZFMf8x7cH/EEQPttpJY91AD7b/RYXtfmBJY8cxyU2NDIQPH0QAAEGLFIkPtsJEKdhBicBBwfgfRDHARCnARDnQfy0PtsYp8EGJwEHB+B9EMcBEKcBEOdB/FsHqEA+20in6idDB+B8xwinCRDnSfj9Ig8EBSDnLda6DxQFEA3wkDEE57nWOSIuEJJAAAADHAP////9Ii4QkmAAAAMcA/////7j/////6xxmDx9EAABIi4QkkAAAAIkISIuEJJgAAACJKDHASIPEGFteX11BXEFdQV5BX8M=")
	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(MCode_PixelSearch, "ptr", Scan01, "int", Width, "int", Height, "int", Stride1, "uint", ARGB, "int", variation, "int*", x, "int*", y)
	Gdip_UnlockBits(pBitmap, BitmapData1)
	return (E = "") ? -3 : E
}

MCode(mcode)
{
  static e := {1:4, 2:1}, c := (A_PtrSize=8) ? "x64" : "x86"
  if (!regexmatch(mcode, "^([0-9]+),(" c ":|.*?," c ":)([^,]+)", m))
    return
  if (!DllCall("crypt32\CryptStringToBinary", "str", m3, "uint", 0, "uint", e[m1], "ptr", 0, "uint*", s, "ptr", 0, "ptr", 0))
    return
  p := DllCall("GlobalAlloc", "uint", 0, "ptr", s, "ptr")
  if (c="x64")
    DllCall("VirtualProtect", "ptr", p, "ptr", s, "uint", 0x40, "uint*", op)
  if (DllCall("crypt32\CryptStringToBinary", "str", m3, "uint", 0, "uint", e[m1], "ptr", p, "uint*", s, "ptr", 0, "ptr", 0))
    return p
  DllCall("GlobalFree", "ptr", p)
}
c++ function (c++ I dont know much)

Code: Select all

int Gdip_PixelSearch(unsigned int* HayStack, int w, int h, int Stride, int ARGB, int variation, int* x, int* y)
{
	int var;
	int offset = Stride / 4;
	for (int y1 = 0; y1 < h; ++y1)
	{
		for (int x1 = 0; x1 < w; ++x1)
		{
			var = (HayStack[x1 + (y1 * offset)] & 0xff) - (ARGB & 0xff);
			if (abs(var) <= variation)
			{
				var = ((HayStack[x1 + (y1 * offset)] >> 8) & 0xff) - ((ARGB >> 8) & 0xff);
				if (abs(var) <= variation)
				{
					var = ((HayStack[x1 + (y1 * offset)] >> 16) & 0xff) - ((ARGB >> 16) & 0xff);
					if (abs(var) <= variation)
					{
						x[0] = x1; y[0] = y1;
						return 0;
					}
				}
			}
		}
	}
	x[0] = -1; y[0] = -1;
	return -1;
}

User avatar
ibieel
Posts: 216
Joined: 17 Oct 2021, 23:30

Re: Gdip_PixelSearch Wont Work :(

Post by ibieel » 23 Oct 2021, 18:28

malcev wrote:
20 Oct 2021, 21:58
Searching using machine code

Code: Select all

setbatchlines -1
pToken := Gdip_Startup()
pBitmap := Gdip_BitmapFromScreen()
if !Gdip_PixelSearch(pBitmap, ARGB := 0xFF336EA5, x, y, 30)
   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, variation)
{
	static MCode_PixelSearch
	if !MCode_PixelSearch
           MCode_PixelSearch := MCode("2,x86:VVdWU4PsEItMJDCLRCQ0i3QkOIXJjVEDD0nRi0wkLMH6AoXJD46RAAAAicEPtvzHRCQIAAAAAA+26MH5EIk8JA+2+Yl8JASNPJUAAAAAiXwkDIt8JCSLRCQoMcmFwH5IixSPD7bCKeiJw8H7HzHYKdg58H8qD7bGKwQkicPB+x8x2CnYOfB/F8HqEA+20itUJASJ0MH4HzHCKcI58n5Bg8EBOUwkKHW4g0QkCAEDfCQMi0QkCDlEJCx1m4tEJDzHAP////+LRCRAxwD/////g8QQuP////9bXl9dw410JgCLRCQ8i3wkCIkIi0QkQIk4g8QQMcBbXl9dww==,x64:QVdBVkFVQVRVV1ZTSIPsGESLlCSIAAAARYXJQY1BA0GJ1UmJzEQPSMiLlCSAAAAARYnGQcH5AkSJTCQMRYXAD46KAAAAidcPtvZFMf8x7cH/EEQPttpJY91AD7b/RYXtfmBJY8cxyU2NDIQPH0QAAEGLFIkPtsJEKdhBicBBwfgfRDHARCnARDnQfy0PtsYp8EGJwEHB+B9EMcBEKcBEOdB/FsHqEA+20in6idDB+B8xwinCRDnSfj9Ig8EBSDnLda6DxQFEA3wkDEE57nWOSIuEJJAAAADHAP////9Ii4QkmAAAAMcA/////7j/////6xxmDx9EAABIi4QkkAAAAIkISIuEJJgAAACJKDHASIPEGFteX11BXEFdQV5BX8M=")
	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(MCode_PixelSearch, "ptr", Scan01, "int", Width, "int", Height, "int", Stride1, "uint", ARGB, "int", variation, "int*", x, "int*", y)
	Gdip_UnlockBits(pBitmap, BitmapData1)
	return (E = "") ? -3 : E
}

MCode(mcode)
{
  static e := {1:4, 2:1}, c := (A_PtrSize=8) ? "x64" : "x86"
  if (!regexmatch(mcode, "^([0-9]+),(" c ":|.*?," c ":)([^,]+)", m))
    return
  if (!DllCall("crypt32\CryptStringToBinary", "str", m3, "uint", 0, "uint", e[m1], "ptr", 0, "uint*", s, "ptr", 0, "ptr", 0))
    return
  p := DllCall("GlobalAlloc", "uint", 0, "ptr", s, "ptr")
  if (c="x64")
    DllCall("VirtualProtect", "ptr", p, "ptr", s, "uint", 0x40, "uint*", op)
  if (DllCall("crypt32\CryptStringToBinary", "str", m3, "uint", 0, "uint", e[m1], "ptr", p, "uint*", s, "ptr", 0, "ptr", 0))
    return p
  DllCall("GlobalFree", "ptr", p)
}
c++ function (c++ I dont know much)

Code: Select all

int Gdip_PixelSearch(unsigned int* HayStack, int w, int h, int Stride, int ARGB, int variation, int* x, int* y)
{
	int var;
	int offset = Stride / 4;
	for (int y1 = 0; y1 < h; ++y1)
	{
		for (int x1 = 0; x1 < w; ++x1)
		{
			var = (HayStack[x1 + (y1 * offset)] & 0xff) - (ARGB & 0xff);
			if (abs(var) <= variation)
			{
				var = ((HayStack[x1 + (y1 * offset)] >> 8) & 0xff) - ((ARGB >> 8) & 0xff);
				if (abs(var) <= variation)
				{
					var = ((HayStack[x1 + (y1 * offset)] >> 16) & 0xff) - ((ARGB >> 16) & 0xff);
					if (abs(var) <= variation)
					{
						x[0] = x1; y[0] = y1;
						return 0;
					}
				}
			}
		}
	}
	x[0] = -1; y[0] = -1;
	return -1;
}
WTF ?!
I can't understand any of this code...
what does that mean?

Code: Select all

  MCode_PixelSearch := MCode("2,x86:VVdWU4PsEItMJDCLRCQ0i3QkOIXJjVEDD0nRi0wkLMH6AoXJD46RAAAAicEPtvzHRCQIAAAAAA+26MH5EIk8JA+2+Yl8JASNPJUAAAAAiXwkDIt8JCSLRCQoMcmFwH5IixSPD7bCKeiJw8H7HzHYKdg58H8qD7bGKwQkicPB+x8x2CnYOfB/F8HqEA+20itUJASJ0MH4HzHCKcI58n5Bg8EBOUwkKHW4g0QkCAEDfCQMi0QkCDlEJCx1m4tEJDzHAP////+LRCRAxwD/////g8QQuP////9bXl9dw410JgCLRCQ8i3wkCIkIi0QkQIk4g8QQMcBbXl9dww==,x64:QVdBVkFVQVRVV1ZTSIPsGESLlCSIAAAARYXJQY1BA0GJ1UmJzEQPSMiLlCSAAAAARYnGQcH5AkSJTCQMRYXAD46KAAAAidcPtvZFMf8x7cH/EEQPttpJY91AD7b/RYXtfmBJY8cxyU2NDIQPH0QAAEGLFIkPtsJEKdhBicBBwfgfRDHARCnARDnQfy0PtsYp8EGJwEHB+B9EMcBEKcBEOdB/FsHqEA+20in6idDB+B8xwinCRDnSfj9Ig8EBSDnLda6DxQFEA3wkDEE57nWOSIuEJJAAAADHAP////9Ii4QkmAAAAMcA/////7j/////6xxmDx9EAABIi4QkkAAAAIkISIuEJJgAAACJKDHASIPEGFteX11BXEFdQV5BX8M=")
but it seems to be working...


can I change the "Width, Height" parameters to choose which area of the screen I want to search?

is there any way to change the coordinate mode to "Screen" instead of "Window"(default)?



is there any way to know the pixel of the X,Y coordinate?
instead of looking for the color pixel, know what the color of the coordinate pixel
using GDIP

wanted to script this way with GDIP:
identify the color of the pixel under the mouse pointer


Code: Select all

MouseGetPos, MouseX, MouseY
PixelGetColor, ColorID, %MouseX%, %MouseY%
ToolTip, %ColorID%
Last edited by ibieel on 23 Oct 2021, 20:42, edited 4 times in total.

Post Reply

Return to “Ask for Help (v1)”