BeBoP Posted January 9, 2012 Share Posted January 9, 2012 (edited) Hello, I wrote a simple function to read a pixel color from a bitmap saved in memory. The method I use takes the screenshot of a window ignoring any windows that might be on top. This is important because I want to code to work while the window is in the background(not minimized). This is why will not work for me. Would it be possible for me to some how use parts of that UDF and maintain the method of obtaining the snapshot or is there some other way I can search for a pixel in memory.Here is the code I have so far.#include <WinAPI.au3> $winHandle = WinGetHandle("[CLASS:Notepad]") ConsoleWrite(memoryReadPixel(10,10,$winHandle) & @CRLF) Func memoryReadPixel($x,$y,$handle) $hDC = _WinAPI_GetWindowDC($handle) $iColor = DllCall("gdi32.dll","int","GetPixel","int",$hDC,"int",$x,"int",$y) $sColor = Hex($iColor[0],6) _WinAPI_ReleaseDC($handle, $hDC) Return Hex("0x" & StringRight($sColor,2) & StringMid($sColor,3,2) & StringLeft($sColor,2)) EndFuncI have tried using for loops with x,y,width,height checking each pixel in-between but those were much too slow.Thanks for any help. Edited January 9, 2012 by BeBoP Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted January 9, 2012 Moderators Share Posted January 9, 2012 BeBoP, Take a look at this from Manadar - it might help you. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
BeBoP Posted January 9, 2012 Author Share Posted January 9, 2012 Thanks for the help Melba23 but Manadar's script can only check single pixels. I need the script to be able to preform a search. Checking every pixel is just not very efficient. Link to comment Share on other sites More sharing options...
Yashied Posted January 9, 2012 Share Posted January 9, 2012 If do not take into account the time for creating a screenshot, the _PixelSearchEx() is approximately 4 times faster than native PixelSearch(). To run this example you will need to download a library.expandcollapse popup#Include <ScreenCapture.au3> #Include <WinAPIEx.au3> $Timer = TimerInit() $Pos = PixelSearch(0, 0, @DesktopWidth, @DesktopHeight, 0x00FFFF) ConsoleWrite('PixelSearch() => X = ' & $Pos[0] & ', Y = ' & $Pos[1] & ', Time = ' & TimerDiff($Timer) & @CR & @CR) $hBitmap = _ScreenCapture_Capture() $Timer = TimerInit() $Pos = _PixelSearchEx($hBitmap, 0x00FFFF) ConsoleWrite('PixelSearchEx() => X = ' & $Pos[0] & ', Y = ' & $Pos[1] & ', Time = ' & TimerDiff($Timer) & @CR) Func _PixelSearchEx($hBitmap, $iColor) Static $pProc = 0 If Not $pProc Then If @AutoItX64 Then ; Not implemented! Return SetError(1, 0, 0) Else $pProc = __Init(Binary( _ '0x5553575631C0505050837C24280074118B6C2428837D14007407B801000000EB' & _ '0231C021C0747F8B6C24288B5D084B891C2431C03B04247F6DC7442404000000' & _ '008B6C24288B5D044B3B5C24047C528B6C24288B5D148B7C24048B34248B6C24' & _ '280FAF750401F7C1E70201FB895C24088B6C24088B5D0081E3FFFFFF003B5C24' & _ '2475188B5C24048B6C24288B7D082B3C244FC1E71009FB89D8EB14FF44240471' & _ 'A0FF0C24718CB8FFFFFFFFEB0231C083C40C5E5F5B5DC21000')) EndIf EndIf Local $tDIB, $tInt, $tPos, $aPos, $Ret, $Error = True $hBitmap = _WinAPI_CopyBitmap($hBitmap) If @error Then Return SetError(1, 0, 0) EndIf Do $tDIB = DllStructCreate($tagDIBSECTION) If (Not _WinAPI_GetObject($hBitmap, DllStructGetSize($tDIB), DllStructGetPtr($tDIB))) Or (DllStructGetData($tDIB, 'bmBitsPixel') <> 32) Or (DllStructGetData($tDIB, 'biCompression')) Then ExitLoop EndIf $Ret = _WinAPI_CallWindowProc($pProc, 0, $iColor, DllStructGetPtr($tDIB), 0) If (@error) Or ($Ret = -1) Then ExitLoop EndIf $Error = False Until 1 _WinAPI_DeleteObject($hBitmap) If $Error Then Return SetError(1, 0, 0) EndIf $tInt = DllStructCreate('int') $tPos = DllStructCreate('ushort;ushort', DllStructGetPtr($tInt)) DllStructSetData($tInt, 1, $Ret) Dim $aPos[2] For $i = 0 To 1 $aPos[$i] = DllStructGetData($tPos, $i + 1) Next Return $aPos EndFunc ;==>_PixelSearchEx My UDFs: iKey | FTP Uploader | Battery Checker | Boot Manager | Font Viewer | UDF Keyword Manager | Run Dialog Replacement | USBProtect | 3D Axis | Calculator | Sleep | iSwitcher | TM | NetHelper | File Types Manager | Control Viewer | SynFolders | DLL Helper Animated Tray Icons UDF Library | Hotkeys UDF Library | Hotkeys Input Control UDF Library | Caret Shape UDF Library | Context Help UDF Library | Most Recently Used List UDF Library | Icons UDF Library | FTP UDF Library | Script Communications UDF Library | Color Chooser UDF Library | Color Picker Control UDF Library | IPHelper (Vista/7) UDF Library | WinAPI Extended UDF Library | WinAPIVhd UDF Library | Icon Chooser UDF Library | Copy UDF Library | Restart UDF Library | Event Log UDF Library | NotifyBox UDF Library | Pop-up Windows UDF Library | TVExplorer UDF Library | GuiHotKey UDF Library | GuiSysLink UDF Library | Package UDF Library | Skin UDF Library | AITray UDF Library | RDC UDF Library Appropriate path | Button text color | Gaussian random numbers | Header's styles (Vista/7) | ICON resource enumeration | Menu & INI | Tabbed string size | Tab's skin | Pop-up circular menu | Progress Bar without animation (Vista/7) | Registry export | Registry path jumping | Unique hardware ID | Windows alignment More... Link to comment Share on other sites More sharing options...
BeBoP Posted January 9, 2012 Author Share Posted January 9, 2012 (edited) Edit: Scratch that, the script works. The color was simply not being found. Thanks Yashied and Melba. expandcollapse popup#include <WinAPI.au3> #Include <WinAPIEx.au3> #include <Array.au3> $window = "[CLASS:Notepad]" $handle = WinGetHandle($window) $aWinPos = WinGetPos($handle) $hDC = _WinAPI_GetWindowDC($handle) $hMemDC = _WinAPI_CreateCompatibleDC($hDC) $hBitmap = _WinAPI_CreateCompatibleBitmap($hDC, $aWinPos[2], $aWinPos[3]) _WinAPI_SelectObject($hMemDC, $hBitmap) _PrintWindow($handle, $hMemDC) $lookup = _PixelSearchEx($hBitmap, 0x00FFFF) _ArrayDisplay($lookup) _WinAPI_DeleteDC($hMemDC) _WinAPI_ReleaseDC($handle, $hDC) _WinAPI_DeleteObject($hBitmap) Func _PixelSearchEx($hBitmap, $iColor) Static $pProc = 0 If Not $pProc Then If @AutoItX64 Then ; Not implemented! Return SetError(1, 0, 0) Else $pProc = __Init(Binary( _ '0x5553575631C0505050837C24280074118B6C2428837D14007407B801000000EB' & _ '0231C021C0747F8B6C24288B5D084B891C2431C03B04247F6DC7442404000000' & _ '008B6C24288B5D044B3B5C24047C528B6C24288B5D148B7C24048B34248B6C24' & _ '280FAF750401F7C1E70201FB895C24088B6C24088B5D0081E3FFFFFF003B5C24' & _ '2475188B5C24048B6C24288B7D082B3C244FC1E71009FB89D8EB14FF44240471' & _ 'A0FF0C24718CB8FFFFFFFFEB0231C083C40C5E5F5B5DC21000')) EndIf EndIf Local $tDIB, $tInt, $tPos, $aPos, $Ret, $Error = True $hBitmap = _WinAPI_CopyBitmap($hBitmap) If @error Then Return SetError(1, 0, 0) EndIf Do $tDIB = DllStructCreate($tagDIBSECTION) If (Not _WinAPI_GetObject($hBitmap, DllStructGetSize($tDIB), DllStructGetPtr($tDIB))) Or (DllStructGetData($tDIB, 'bmBitsPixel') <> 32) Or (DllStructGetData($tDIB, 'biCompression')) Then ExitLoop EndIf $Ret = _WinAPI_CallWindowProc($pProc, 0, $iColor, DllStructGetPtr($tDIB), 0) If (@error) Or ($Ret = -1) Then ExitLoop EndIf $Error = False Until 1 _WinAPI_DeleteObject($hBitmap) If $Error Then Return SetError(1, 0, 0) EndIf $tInt = DllStructCreate('int') $tPos = DllStructCreate('ushort;ushort', DllStructGetPtr($tInt)) DllStructSetData($tInt, 1, $Ret) Dim $aPos[2] For $i = 0 To 1 $aPos[$i] = DllStructGetData($tPos, $i + 1) Next Return $aPos EndFunc ;==>_PixelSearchEx Func _PrintWindow($hWnd, $hMemDC, $iFlag = 0) $aRet = DllCall("User32.dll", "int", "PrintWindow", _ "hwnd", $hWnd, _ "hwnd", $hMemDC, _ "int", $iFlag) Return $aRet[0] EndFunc ;==>_PrintWindow Edited January 9, 2012 by BeBoP Abo-Mota and Wanarmal 2 Link to comment Share on other sites More sharing options...
Yashied Posted January 10, 2012 Share Posted January 10, 2012 Also, you can use _WinAPI_PrintWindow() instead of _PrintWindow(). My UDFs: iKey | FTP Uploader | Battery Checker | Boot Manager | Font Viewer | UDF Keyword Manager | Run Dialog Replacement | USBProtect | 3D Axis | Calculator | Sleep | iSwitcher | TM | NetHelper | File Types Manager | Control Viewer | SynFolders | DLL Helper Animated Tray Icons UDF Library | Hotkeys UDF Library | Hotkeys Input Control UDF Library | Caret Shape UDF Library | Context Help UDF Library | Most Recently Used List UDF Library | Icons UDF Library | FTP UDF Library | Script Communications UDF Library | Color Chooser UDF Library | Color Picker Control UDF Library | IPHelper (Vista/7) UDF Library | WinAPI Extended UDF Library | WinAPIVhd UDF Library | Icon Chooser UDF Library | Copy UDF Library | Restart UDF Library | Event Log UDF Library | NotifyBox UDF Library | Pop-up Windows UDF Library | TVExplorer UDF Library | GuiHotKey UDF Library | GuiSysLink UDF Library | Package UDF Library | Skin UDF Library | AITray UDF Library | RDC UDF Library Appropriate path | Button text color | Gaussian random numbers | Header's styles (Vista/7) | ICON resource enumeration | Menu & INI | Tabbed string size | Tab's skin | Pop-up circular menu | Progress Bar without animation (Vista/7) | Registry export | Registry path jumping | Unique hardware ID | Windows alignment More... Link to comment Share on other sites More sharing options...
FastFrench Posted January 10, 2012 Share Posted January 10, 2012 (edited) Hello, I wrote a simple function to read a pixel color from a bitmap saved in memory. The method I use takes the screenshot of a window ignoring any windows that might be on top. This is important because I want to code to work while the window is in the background(not minimized). This is why will not work for me. Would it be possible for me to some how use parts of that UDF and maintain the method of obtaining the snapshot or is there some other way I can search for a pixel in memory.Actually, when I started writing FastFind UDF, I initialy wanted to use PrintWindow (to be able to track window content, even when window is hidden, out of screen or minimized) and I've done some experimentations with it. Unfortunately, it appears to be very application dependant, and OS dependant. For instance, Flash applications do not render properly with Printwindow. Also hidden applications appeared to be properly captured in some cases but not in others. That's the reason I prefered not to use Printwindow.So I confirm that FastFind can't capture hidden content in a reliable way. Edited January 10, 2012 by FastFrench Link to comment Share on other sites More sharing options...
Wanarmal Posted October 28, 2017 Share Posted October 28, 2017 (edited) On 1/10/2012 at 2:55 AM, BeBoP said: Edit: Scratch that, the script works. The color was simply not being found. Thanks Yashied and Melba. expandcollapse popup#include <WinAPI.au3> #Include <WinAPIEx.au3> #include <Array.au3> $window = "[CLASS:Notepad]" $handle = WinGetHandle($window) $aWinPos = WinGetPos($handle) $hDC = _WinAPI_GetWindowDC($handle) $hMemDC = _WinAPI_CreateCompatibleDC($hDC) $hBitmap = _WinAPI_CreateCompatibleBitmap($hDC, $aWinPos[2], $aWinPos[3]) _WinAPI_SelectObject($hMemDC, $hBitmap) _PrintWindow($handle, $hMemDC) $lookup = _PixelSearchEx($hBitmap, 0x00FFFF) _ArrayDisplay($lookup) _WinAPI_DeleteDC($hMemDC) _WinAPI_ReleaseDC($handle, $hDC) _WinAPI_DeleteObject($hBitmap) Func _PixelSearchEx($hBitmap, $iColor) Static $pProc = 0 If Not $pProc Then If @AutoItX64 Then ; Not implemented! Return SetError(1, 0, 0) Else $pProc = __Init(Binary( _ '0x5553575631C0505050837C24280074118B6C2428837D14007407B801000000EB' & _ '0231C021C0747F8B6C24288B5D084B891C2431C03B04247F6DC7442404000000' & _ '008B6C24288B5D044B3B5C24047C528B6C24288B5D148B7C24048B34248B6C24' & _ '280FAF750401F7C1E70201FB895C24088B6C24088B5D0081E3FFFFFF003B5C24' & _ '2475188B5C24048B6C24288B7D082B3C244FC1E71009FB89D8EB14FF44240471' & _ 'A0FF0C24718CB8FFFFFFFFEB0231C083C40C5E5F5B5DC21000')) EndIf EndIf Local $tDIB, $tInt, $tPos, $aPos, $Ret, $Error = True $hBitmap = _WinAPI_CopyBitmap($hBitmap) If @error Then Return SetError(1, 0, 0) EndIf Do $tDIB = DllStructCreate($tagDIBSECTION) If (Not _WinAPI_GetObject($hBitmap, DllStructGetSize($tDIB), DllStructGetPtr($tDIB))) Or (DllStructGetData($tDIB, 'bmBitsPixel') <> 32) Or (DllStructGetData($tDIB, 'biCompression')) Then ExitLoop EndIf $Ret = _WinAPI_CallWindowProc($pProc, 0, $iColor, DllStructGetPtr($tDIB), 0) If (@error) Or ($Ret = -1) Then ExitLoop EndIf $Error = False Until 1 _WinAPI_DeleteObject($hBitmap) If $Error Then Return SetError(1, 0, 0) EndIf $tInt = DllStructCreate('int') $tPos = DllStructCreate('ushort;ushort', DllStructGetPtr($tInt)) DllStructSetData($tInt, 1, $Ret) Dim $aPos[2] For $i = 0 To 1 $aPos[$i] = DllStructGetData($tPos, $i + 1) Next Return $aPos EndFunc ;==>_PixelSearchEx Func _PrintWindow($hWnd, $hMemDC, $iFlag = 0) $aRet = DllCall("User32.dll", "int", "PrintWindow", _ "hwnd", $hWnd, _ "hwnd", $hMemDC, _ "int", $iFlag) Return $aRet[0] EndFunc ;==>_PrintWindow The code is good working. But I want to search in rectangle area of Notepad Program. How to do this? Edited October 28, 2017 by Wanarmal Link to comment Share on other sites More sharing options...
Wanarmal Posted October 29, 2017 Share Posted October 29, 2017 Yeh!! I got it. I use _WinAPI_BitBlt fuction. Thank you for code from this topic. It the best code of pixel search. Link to comment Share on other sites More sharing options...
Sw4rM Posted October 28, 2018 Share Posted October 28, 2018 Thx a lot Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now