PixelSearch Slow with Windows 10 and MEmu / BlueStacks

01/22/2017 21:41 WnG88#1
I've got a pixelsearch script that worked well until I upgraded from Windows 7 to Windows 10. In Windows 7, I had disabled Aero. Windows 10, however does not allow disabling Desktop Composition which is slowing down PixelSearch.

I'm looking to learn how to speed up pixelsearch so that I can use ControlClick to click on an Android Emulator - currently using MEmu. Can anyone provide a short example of using GDIPlus if that is the faster route to go? With PixelSearch a small 100x100 search takes a few seconds which is too slow for me.

I would like to do the following:
- PixelSearch in a control a certain color and get the coords,
- ControlClick the found coordinate
- this search would need to happen several times in a second or 2

Any help would be appreciated. I've pulled various GDI examples, but I'm too noob to get an efficient way to do it.

Thanks.
01/23/2017 22:26 Moneypulation#2
Quote:
Originally Posted by WnG88 View Post
With PixelSearch a small 100x100 search takes a few seconds which is too slow for me.
That's really, really slow. Do you have an average PC or a cheaper one? If you show us the PixelSearch part of your code, maybe we can find some efficiency optimization but if it takes that long, I'm afraid it must be down to your slow PC
01/23/2017 23:18 FacePalmMan#3
There you are. This should work faster in this case.

Code:
#include <GDIPlus.au3>
_GDIPlus_Startup()
$hGUI = GUICreate("Test", 100, 100)
GUISetState()
Global $ahGraphics[3] = [_GDIPlus_GraphicsCreateFromHWND($hGUI)]
$ahGraphics[1] = _GDIPlus_BitmapCreateFromGraphics(100, 100, $ahGraphics[0])
$ahGraphics[2] = _GDIPlus_ImageGetGraphicsContext($ahGraphics[1])

While GUIGetMsg() <> -3
	_GDIPlus_GraphicsClear($ahGraphics[2], 0xFFFFFFFF)
	$hBitmap = _ScreenCapture_Capture("", 0, 0, 100, 100, WinGetHandle("[active]"))

	_GDIPlus_GraphicsDrawImage($ahGraphics[2], $hBitmap , 0, 0)
	_GDIPlus_GraphicsDrawImage($ahGraphics[0], $ahGraphics[1], 0, 0)
WEnd

Func __Pixelsearchbitmap($hBitmap, $iW, $iH, $iColor, $iTolerance = 0)
	Local $i, $j, $iColor2, $iRed, $iGreen, $iBlue, $aRGB[3] = [Dec(StringLeft(Hex($iColor, 6), 2)), Dec(StringLeft(Hex($iColor, 4), 2)), Dec(Hex($iColor, 2))], $aRet[2]
	For $i = 0 To $iW - 1
		For $j = 0 To $iH - 1
			$iColor2 = _GDIPlus_BitmapGetPixel($hBitmap, $i, $j)
			$iRed = Dec(StringLeft(Hex($iColor2, 6), 2))
			$iGreen = Dec(StringLeft(Hex($iColor2, 4), 2))
			$iBlue = Dec(Hex($iColor2, 2))
			If Abs($aRGB[0] - $iRed) + Abs($aRGB[1] - $iGreen) + Abs($aRGB[2] - $iBlue) < $iTolerance Then
				$aRet[0] = $i
				$aRet[1] = $j
				Return $aRet
			EndIf
		Next
	Next
	Return SetError(1, 1, False)
EndFunc



Func _ScreenCapture_Capture($sFileName = "", $iLeft = 0, $iTop = 0, $iRight = -1, $iBottom = -1, $hWindow = 0)
	If $iRight = -1 Then $iRight = _WinAPI_GetSystemMetrics(0) - 1
	If $iBottom = -1 Then $iBottom = _WinAPI_GetSystemMetrics(1) - 1
	If $iRight < $iLeft Then Return SetError(-1, 0, 0)
	If $iBottom < $iTop Then Return SetError(-2, 0, 0)

	Local $iW = ($iRight - $iLeft) + 1
	Local $iH = ($iBottom - $iTop) + 1
	Local $hWnd = _WinAPI_GetDesktopWindow()
	If $hWindow = 0 Then
		Local $hDDC = _WinAPI_GetDC($hWnd)
	Else
		Local $hDDC = _WinAPI_GetDC($hWindow)
	EndIf
	Local $hCDC = _WinAPI_CreateCompatibleDC($hDDC)
	Local $hBMP = _WinAPI_CreateCompatibleBitmap($hDDC, $iW, $iH)
	_WinAPI_SelectObject($hCDC, $hBMP)
	_WinAPI_BitBlt($hCDC, 0, 0, $iW, $iH, $hDDC, $iLeft, $iTop, 0x00CC0020)

	_WinAPI_ReleaseDC($hWnd, $hDDC)
	_WinAPI_DeleteDC($hCDC)

	Return _GDIPlus_BitmapCreateFromHBITMAP($hBMP)

EndFunc   ;==>_ScreenCapture_Capture
Is that understandable enough for you to use? I took the _screencapture_capture function and modified it to return a bitmap, and to be able to view a window you choose (in this example: WinGetHandle("[active]") (the window that is currently active (try switching windows and see how it changes too))). Just replace "[Active]" with the name of the Bluestacks window, and write your bot code into the while loop. Feel free to remove the GUI, and all lines that have $ahGraphics[0]/[1]/[2] in them.
02/04/2017 01:57 WnG88#4
Thanks Guys!

Trying the code out. I have an i7 4700k - so cpu isn't the prob. Everything i researched says it is related to Windows 10 and Desktop Composition (AERO) which slows down PixelSearch. I'm trying out the GDI code now and will update.