Use GDI+ to do image search?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jollyjoe
Posts: 42
Joined: 13 Feb 2021, 18:51

Re: Use GDI+ to do image search?

Post by jollyjoe » 21 Mar 2021, 08:05

another update question, Why is it that when I pass my code to my friends, the needle image is not recognized? but if they take a screenshot of their own needle, it works fine. Is there any solution to this? I already set the variation to 100-250 (i've been experimenting with this. Thanks so much again for the reply

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

Re: Use GDI+ to do image search?

Post by boiler » 21 Mar 2021, 08:18

It could by any of a number of reasons. It could be the result of anti-aliased text being rendered differently in different instances (most likely, I think). It may be that their graphics card renders it differently. It could be the application renders it differently in different situations such as scaling for screen resolution. Increasing the allowable variation won’t be good enough if the pixels are fundamentally different, which can and does happen in situations like these.

If they send you their needle images and you zoom in on theirs and yours in MS Paint (or some other picture viewer that doesn’t distort the image like the standard picture viewer does), then you will see the differences between the images if you look closely.

jollyjoe
Posts: 42
Joined: 13 Feb 2021, 18:51

Re: Use GDI+ to do image search?

Post by jollyjoe » 21 Mar 2021, 16:01

That makes alot of sense, thank you so much for replying

MamboWumbo
Posts: 16
Joined: 15 Jul 2021, 21:08

Re: Use GDI+ to do image search?

Post by MamboWumbo » 21 Jul 2021, 01:56

Could you share your final script, please? I was learning GDI+ too, I want to know how the successful script running with GDI, I will be appreciate if u want to share the simple tutorial too :thumbup: thanks

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

Re: Use GDI+ to do image search?

Post by ibieel » 17 Oct 2021, 23:39

boiler wrote:
16 Mar 2021, 08:31
Try replacing Gdip_BitmapFromHWND() with this version which allows for more graphics rendering approaches to be captured:

Code: Select all

Gdip_BitmapFromHWND(hwnd)
{
	WinGetPos,,, Width, Height, ahk_id %hwnd%
	hbm := CreateDIBSection(Width, Height), hdc := CreateCompatibleDC(), obm := SelectObject(hdc, hbm)
	RegExMatch(A_OsVersion, "\d+", Version)
	PrintWindow(hwnd, hdc, Version >= 8 ? 2 : 0)
	pBitmap := Gdip_CreateBitmapFromHBITMAP(hbm)
	SelectObject(hdc, obm), DeleteObject(hbm), DeleteDC(hdc)
	return pBitmap
}
my "Gdip_BitmapFromHWND" is returning 0

I've already tried to modify the library as you said, but it still keeps returning 0

when I go to use Gdip_ImageSearch it returns "-1001", claiming there is a problem with Haystack

can you help me? my code is:

Code: Select all

#NoEnv
#SingleInstance Force
SetBatchLines, -1
Process, Priority,, High

#Include GDIP.ahk
#Include Gdip_ImageSearch.ahk ; (this is masterfocus' gdip_imagesearch.ahk)

3::
	ptok := Gdip_startup()
	
	win := Gdip_BitmapFromHWND( WinExist("ahk_class MSPaintApp") )	;Haystack is paint open with png  Needle (testimg.png)
	pic := Gdip_createBitmapFromFile("testimg.png")
	err := Gdip_imageSearch( win, pic, xy, 0,0,0,0, 15, ,1,0 )
	
	Gdip_disposeImage( win )
	Gdip_disposeImage( pic )
	Gdip_shutdown(ptok)
return

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

Re: Use GDI+ to do image search?

Post by boiler » 18 Oct 2021, 03:17

What do you get if you have that window open and run the following?

Code: Select all

MsgBox, % WinExist("ahk_class MSPaintApp")

Are you sure it’s not the one that is getting the image from a file (as opposed to the one from the MSPaint window) that is returning a 0? Use the full path to the file, not just the file name.

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

Re: Use GDI+ to do image search?

Post by ibieel » 18 Oct 2021, 15:09

boiler wrote:
18 Oct 2021, 03:17
What do you get if you have that window open and run the following?

Code: Select all

MsgBox, % WinExist("ahk_class MSPaintApp")

Are you sure it’s not the one that is getting the image from a file (as opposed to the one from the MSPaint window) that is returning a 0? Use the full path to the file, not just the file name.
it looks like the GDIP I was using only works on 32Bits systems (not sure)
I used another GDIP library and it worked. can you analyze it for me and see what the difference between the two is? I'm not so good at it :?

GDIP LIBRARY ERROR (OLD):
https://raw.githubusercontent.com/tariqporter/Gdip/master/Gdip.ahk


GIP LIBRARY WORKING (NEW):
https://raw.githubusercontent.com/berban/Gdip/master/Gdip.ahk

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

Re: Use GDI+ to do image search?

Post by ibieel » 18 Oct 2021, 15:21

boiler wrote:
18 Oct 2021, 03:17
What do you get if you have that window open and run the following?

Code: Select all

MsgBox, % WinExist("ahk_class MSPaintApp")

Are you sure it’s not the one that is getting the image from a file (as opposed to the one from the MSPaint window) that is returning a 0? Use the full path to the file, not just the file name.
another question too, could you explain to me what the command is for: "Gdip_startup()" and "Gdip_disposeImage()" and "Gdip_shutdown()"
THXX

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

Re: Use GDI+ to do image search?

Post by boiler » 19 Oct 2021, 06:23

ibieel wrote:
18 Oct 2021, 15:09
it looks like the GDIP I was using only works on 32Bits systems (not sure)
I used another GDIP library and it worked. can you analyze it for me and see what the difference between the two is? I'm not so good at it :?
Are you asking me to analyze the changes that were made from one to the other to make it 64-bit compatible? What would you do with that information? I like to help where I can, but I’m not going to take the time to do that as I don’t see the value in it. Maybe someone else wouldn’t mind doing that for you.

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

Re: Use GDI+ to do image search?

Post by boiler » 19 Oct 2021, 06:32

ibieel wrote:
18 Oct 2021, 15:21
another question too, could you explain to me what the command is for: "Gdip_startup()" and "Gdip_disposeImage()" and "Gdip_shutdown()"
It’s the way the Microsoft’s GDI+ functions work. You must initialize GDI+ before you can call any of its functions, then you have to delete the GDI+ objects before shutting down or else they’ll stay in memory for no reason, causing you to have less available RAM for other applications. Then you have to shut it down to have GDI+ clean up the other resources it put in place from memory.

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

Re: Use GDI+ to do image search?

Post by ibieel » 20 Oct 2021, 00:14

boiler wrote:
19 Oct 2021, 06:23
ibieel wrote:
18 Oct 2021, 15:09
it looks like the GDIP I was using only works on 32Bits systems (not sure)
I used another GDIP library and it worked. can you analyze it for me and see what the difference between the two is? I'm not so good at it :?
Are you asking me to analyze the changes that were made from one to the other to make it 64-bit compatible? What would you do with that information? I like to help where I can, but I’m not going to take the time to do that as I don’t see the value in it. Maybe someone else wouldn’t mind doing that for you.
No no, I wanted to know if this was really the problem

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

Re: Use GDI+ to do image search?

Post by ibieel » 20 Oct 2021, 00:16

boiler wrote:
19 Oct 2021, 06:32
ibieel wrote:
18 Oct 2021, 15:21
another question too, could you explain to me what the command is for: "Gdip_startup()" and "Gdip_disposeImage()" and "Gdip_shutdown()"
It’s the way the Microsoft’s GDI+ functions work. You must initialize GDI+ before you can call any of its functions, then you have to delete the GDI+ objects before shutting down or else they’ll stay in memory for no reason, causing you to have less available RAM for other applications. Then you have to shut it down to have GDI+ clean up the other resources it put in place from memory.
I used the command:
Gdip_disposeImage(imagebitmap)

however, when I called
msgbox, %imagebitmap%

the image bitmap was still allocated in memory

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

Re: Use GDI+ to do image search?

Post by boiler » 20 Oct 2021, 04:16

ibieel wrote:
20 Oct 2021, 00:14
No no, I wanted to know if this was really the problem
Apparently so.

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

Re: Use GDI+ to do image search?

Post by boiler » 20 Oct 2021, 04:35

ibieel wrote:
20 Oct 2021, 00:16
I used the command:
Gdip_disposeImage(imagebitmap)

however, when I called
msgbox, %imagebitmap%

the image bitmap was still allocated in memory
And what has told you that the variable you used to pass the image pointer to the function would change by calling the function? (Hint: Nothing)

The way you would know if it was successfully released is when the return value of the function call is 0:

Code: Select all

MsgBox, % Gdip_disposeImage(imagebitmap)

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

Re: Use GDI+ to do image search?

Post by ibieel » 28 Oct 2021, 16:48

boiler wrote:
16 Mar 2021, 08:31
Try replacing Gdip_BitmapFromHWND() with this version which allows for more graphics rendering approaches to be captured:

Code: Select all

Gdip_BitmapFromHWND(hwnd)
{
	WinGetPos,,, Width, Height, ahk_id %hwnd%
	hbm := CreateDIBSection(Width, Height), hdc := CreateCompatibleDC(), obm := SelectObject(hdc, hbm)
	RegExMatch(A_OsVersion, "\d+", Version)
	PrintWindow(hwnd, hdc, Version >= 8 ? 2 : 0)
	pBitmap := Gdip_CreateBitmapFromHBITMAP(hbm)
	SelectObject(hdc, obm), DeleteObject(hbm), DeleteDC(hdc)
	return pBitmap
}
was using a different version of Gdip_BitmapFromHWND()
could you explain to a noob like me what is the difference between the one you sent and the one in the script Gdip_All?
what is the need for this line of code?

Code: Select all

RegExMatch(A_OsVersion, "\d+", Version)
is that why some computers were having trouble finding the image in ImageSearch?
I'll start using the one you sent, thanks

please, help me:
viewtopic.php?f=76&t=96073&hilit=Gdip+image+search

doubledave22
Posts: 343
Joined: 08 Jun 2019, 17:36

Re: Use GDI+ to do image search?

Post by doubledave22 » 28 Oct 2021, 17:27

Code: Select all

RegExMatch(A_OsVersion, "\d+", Version)
PrintWindow(hwnd, hdc, Version >= 8 ? 2 : 0)
This says if Windows version is Win 8 or higher then use the 0x2 flag which is an undocumented flag called PW_RENDERFULLCONTENT. https://stackoverflow.com/a/40042587

I believe this helps get pixel data from hardware accelerated windows. This flag will cause printwindow to fail on Win7 and below so you have to set it to 0 for those OS. I posted in your other thread but saving the bitmap to file can help identify whether this flag is needed for your application windows. If the bitmap shows up as all black you may need this flag.

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

Re: Use GDI+ to do image search?

Post by ibieel » 28 Oct 2021, 17:52

doubledave22 wrote:
28 Oct 2021, 17:27

Code: Select all

RegExMatch(A_OsVersion, "\d+", Version)
PrintWindow(hwnd, hdc, Version >= 8 ? 2 : 0)
This says if Windows version is Win 8 or higher then use the 0x2 flag which is an undocumented flag called PW_RENDERFULLCONTENT. https://stackoverflow.com/a/40042587

I believe this helps get pixel data from hardware accelerated windows. This flag will cause printwindow to fail on Win7 and below so you have to set it to 0 for those OS. I posted in your other thread but saving the bitmap to file can help identify whether this flag is needed for your application windows. If the bitmap shows up as all black you may need this flag.
how can i set to 0 on win7 operating systems?

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

Re: Use GDI+ to do image search?

Post by boiler » 28 Oct 2021, 18:32

You don’t set it. That statement sets it. He was describing what it does because you asked, not something more that you have to do.

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

Re: Use GDI+ to do image search?

Post by malcev » 28 Oct 2021, 21:11

Code: Select all

PrintWindow(hwnd, hdc, Version >= 8 ? 2 : 0)
It is not right, because on win8 PW_RENDERFULLCONTENT does not work.
Also PrintWindow is slower and needed only for windows that use hardware acceleration and that are not visible (lies under others).

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

Re: Use GDI+ to do image search?

Post by ibieel » 28 Oct 2021, 21:18

malcev wrote:
28 Oct 2021, 21:11

Code: Select all

PrintWindow(hwnd, hdc, Version >= 8 ? 2 : 0)
It is not right, because on win8 PW_RENDERFULLCONTENT does not work.
Also PrintWindow is slower and needed only for windows that use hardware acceleration and that are not visible (lies under others).
I'm confused so better not use?

Post Reply

Return to “Ask for Help (v1)”