memerim Posted December 18, 2019 Share Posted December 18, 2019 (edited) expandcollapse popup#include <GDIPlus.au3> #include <WinAPISysWin.au3> _GDIPlus_Startup() Global $handle = WinGetHandle("Firefox") Global $hBitmap = Capture_Window($handle, _WinAPI_GetWindowWidth($handle), _WinAPI_GetWindowHeight($handle)) _GDIPlus_ImageSaveToFile($hBitmap, @ScriptDir & "\test.jpg") _GDIPlus_Shutdown() ;ShellExecute(@ScriptDir & "\test.jpg") Func Capture_Window($hWnd, $w, $h) If Not IsHWnd($hWnd) Then Return SetError(1, 0, 0) If Int($w) < 1 Then Return SetError(2, 0, 0) If Int($h) < 1 Then Return SetError(3, 0, 0) Local Const $hDC_Capture = _WinAPI_GetDC(HWnd($hWnd)) Local Const $hMemDC = _WinAPI_CreateCompatibleDC($hDC_Capture) Local Const $hHBitmap = _WinAPI_CreateCompatibleBitmap($hDC_Capture, $w, $h) Local Const $hObjectOld = _WinAPI_SelectObject($hMemDC, $hHBitmap) DllCall("gdi32.dll", "int", "SetStretchBltMode", "hwnd", $hDC_Capture, "uint", 4) DllCall("user32.dll", "int", "PrintWindow", "hwnd", $hWnd, "handle", $hMemDC, "int", 0) _WinAPI_DeleteDC($hMemDC) _WinAPI_SelectObject($hMemDC, $hObjectOld) _WinAPI_ReleaseDC($hWnd, $hDC_Capture) Local Const $hFullScreen = WinGetHandle("[TITLE:Program Manager;CLASS:Progman]") Local Const $aFullScreen = WinGetPos($hFullScreen) Local Const $c1 = $aFullScreen[2] - @DesktopWidth, $c2 = $aFullScreen[3] - @DesktopHeight Local Const $wc1 = $w - $c1, $hc2 = $h - $c2 If (($wc1 > 1 And $wc1 < $w) Or ($w - @DesktopWidth > 1) Or ($hc2 > 7 And $hc2 < $h) Or ($h - @DesktopHeight > 1)) And (BitAND(WinGetState(HWnd($hWnd)), 32) = 32) Then Local $hBmp_t = _GDIPlus_BitmapCreateFromHBITMAP($hHBitmap) $hBmp = _GDIPlus_BitmapCloneArea($hBmp_t, 8, 8, $w - 16, $h - 16) _GDIPlus_BitmapDispose($hBmp_t) Else $hBmp = _GDIPlus_BitmapCreateFromHBITMAP($hHBitmap) EndIf _WinAPI_DeleteObject($hHBitmap) Return $hBmp EndFunc ;==>Capture_Window I'm using this script from @UEZ to Screenshots capture background windows Is possible capture just a area of the handle instead whole screen? Edited January 17, 2020 by memerim Link to comment Share on other sites More sharing options...
memerim Posted December 19, 2019 Author Share Posted December 19, 2019 (edited) expandcollapse popup#include <GDIPlus.au3> #include <ScreenCapture.au3> #include <WinAPIHObj.au3> Local $handle = WinGetHandle("Sem título - Paint") Local $hClone, $hImage, $iX, $i ; Initialize GDI+ library _GDIPlus_Startup() Local $hBitmap = Capture_Window($handle, _WinAPI_GetWindowWidth($handle), _WinAPI_GetWindowHeight($handle)) ;ConsoleWrite(_WinAPI_GetWindowWidth($handle)) _GDIPlus_ImageSaveToFile($hBitmap, @ScriptDir & "\test.bmp") $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap) ; Create 24 bit bitmap clone $iX = _GDIPlus_ImageGetWidth($hImage) $iY = _GDIPlus_ImageGetHeight($hImage) $hClone = _GDIPlus_BitmapCloneArea($hImage, 0, 0, $iX, $iY, $GDIP_PXF24RGB) _GDIPlus_ImageSaveToFile($hClone, @ScriptDir & "\GDIPlus_Image.bmp") ; Clean up resources _GDIPlus_ImageDispose($hClone) _GDIPlus_ImageDispose($hImage) _WinAPI_DeleteObject($hBitmap) ; Shut down GDI+ library _GDIPlus_Shutdown() Func Capture_Window($hWnd, $w, $h) If Not IsHWnd($hWnd) Then Return SetError(1, 0, 0) If Int($w) < 1 Then Return SetError(2, 0, 0) If Int($h) < 1 Then Return SetError(3, 0, 0) Local Const $hDC_Capture = _WinAPI_GetDC(HWnd($hWnd)) Local Const $hMemDC = _WinAPI_CreateCompatibleDC($hDC_Capture) Local Const $hHBitmap = _WinAPI_CreateCompatibleBitmap($hDC_Capture, $w, $h) Local Const $hObjectOld = _WinAPI_SelectObject($hMemDC, $hHBitmap) DllCall("gdi32.dll", "int", "SetStretchBltMode", "hwnd", $hDC_Capture, "uint", 4) DllCall("user32.dll", "int", "PrintWindow", "hwnd", $hWnd, "handle", $hMemDC, "int", 0) _WinAPI_DeleteDC($hMemDC) _WinAPI_SelectObject($hMemDC, $hObjectOld) _WinAPI_ReleaseDC($hWnd, $hDC_Capture) Local Const $hFullScreen = WinGetHandle("[TITLE:Program Manager;CLASS:Progman]") Local Const $aFullScreen = WinGetPos($hFullScreen) Local Const $c1 = $aFullScreen[2] - @DesktopWidth, $c2 = $aFullScreen[3] - @DesktopHeight Local Const $wc1 = $w - $c1, $hc2 = $h - $c2 If (($wc1 > 1 And $wc1 < $w) Or ($w - @DesktopWidth > 1) Or ($hc2 > 7 And $hc2 < $h) Or ($h - @DesktopHeight > 1)) And (BitAND(WinGetState(HWnd($hWnd)), 32) = 32) Then Local $hBmp_t = _GDIPlus_BitmapCreateFromHBITMAP($hHBitmap) $hBmp = _GDIPlus_BitmapCloneArea($hBmp_t, 8, 8, $w - 16, $h - 16) _GDIPlus_BitmapDispose($hBmp_t) Else $hBmp = _GDIPlus_BitmapCreateFromHBITMAP($hHBitmap) EndIf _WinAPI_DeleteObject($hHBitmap) Return $hBmp EndFunc ;==>Capture_Window The script fromy @UEZ saves the bmp in 32bits, i've been trying convert it to 24bits to use with my bmp search script, as it looks for 24bits bmps. Using script above from help file: https://www.autoitscript.com/autoit3/docs/libfunctions/_GDIPlus_BitmapCloneArea.htm But it never saves the cloned 24bits bitmap Edited December 19, 2019 by memerim Link to comment Share on other sites More sharing options...
KaFu Posted December 19, 2019 Share Posted December 19, 2019 expandcollapse popup#include <GDIPlus.au3> #include <ScreenCapture.au3> #include <WinAPIHObj.au3> Local $handle = WinGetHandle("[CLASS:SciTEWindow]", "") Local $hClone, $hImage, $iX, $i ; Initialize GDI+ library _GDIPlus_Startup() Local $hBitmap = Capture_Window($handle, _WinAPI_GetWindowWidth($handle), _WinAPI_GetWindowHeight($handle)) ;ConsoleWrite(_WinAPI_GetWindowWidth($handle)) _GDIPlus_ImageSaveToFile($hBitmap, @ScriptDir & "\test.bmp") ; Create 24 bit bitmap clone $iX = _GDIPlus_ImageGetWidth($hBitmap) $iY = _GDIPlus_ImageGetHeight($hBitmap) $hClone = _GDIPlus_BitmapCloneArea($hBitmap, 0, 0, $iX, $iY, $GDIP_PXF24RGB) _GDIPlus_ImageSaveToFile($hClone, @ScriptDir & "\GDIPlus_Image.bmp") ; Clean up resources _GDIPlus_ImageDispose($hClone) _WinAPI_DeleteObject($hBitmap) ; Shut down GDI+ library _GDIPlus_Shutdown() Func Capture_Window($hWnd, $w, $h) If Not IsHWnd($hWnd) Then Return SetError(1, 0, 0) If Int($w) < 1 Then Return SetError(2, 0, 0) If Int($h) < 1 Then Return SetError(3, 0, 0) Local Const $hDC_Capture = _WinAPI_GetDC(HWnd($hWnd)) Local Const $hMemDC = _WinAPI_CreateCompatibleDC($hDC_Capture) Local Const $hHBitmap = _WinAPI_CreateCompatibleBitmap($hDC_Capture, $w, $h) Local Const $hObjectOld = _WinAPI_SelectObject($hMemDC, $hHBitmap) DllCall("gdi32.dll", "int", "SetStretchBltMode", "hwnd", $hDC_Capture, "uint", 4) DllCall("user32.dll", "int", "PrintWindow", "hwnd", $hWnd, "handle", $hMemDC, "int", 0) _WinAPI_DeleteDC($hMemDC) _WinAPI_SelectObject($hMemDC, $hObjectOld) _WinAPI_ReleaseDC($hWnd, $hDC_Capture) Local Const $hFullScreen = WinGetHandle("[TITLE:Program Manager;CLASS:Progman]") Local Const $aFullScreen = WinGetPos($hFullScreen) Local Const $c1 = $aFullScreen[2] - @DesktopWidth, $c2 = $aFullScreen[3] - @DesktopHeight Local Const $wc1 = $w - $c1, $hc2 = $h - $c2 If (($wc1 > 1 And $wc1 < $w) Or ($w - @DesktopWidth > 1) Or ($hc2 > 7 And $hc2 < $h) Or ($h - @DesktopHeight > 1)) And (BitAND(WinGetState(HWnd($hWnd)), 32) = 32) Then Local $hBmp_t = _GDIPlus_BitmapCreateFromHBITMAP($hHBitmap) $hBmp = _GDIPlus_BitmapCloneArea($hBmp_t, 8, 8, $w - 16, $h - 16) _GDIPlus_BitmapDispose($hBmp_t) Else $hBmp = _GDIPlus_BitmapCreateFromHBITMAP($hHBitmap) EndIf _WinAPI_DeleteObject($hHBitmap) Return $hBmp EndFunc ;==>Capture_Window memerim 1 OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2024-Oct-13) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Oct-13) HMW - Hide my Windows (2024-Oct-19) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2024-Oct-20) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
memerim Posted December 19, 2019 Author Share Posted December 19, 2019 (edited) Thank you Kafu, know if its possible capture just a area instead whole handle screen? -EDIT- Im using this function in a loop, and after some time pop a msg saying something like "out of memory", i saw in help many gdi clean up functions, should i use _gdi plus start up in the beggining of each loop and gdi shut down in the end of the loop? Edited December 19, 2019 by memerim Link to comment Share on other sites More sharing options...
KaFu Posted December 19, 2019 Share Posted December 19, 2019 (edited) I think _WinAPI_DeleteObject($hBitmap) did not delete the object resulting in a memory leak, try this with _GDIPlus_ImageDispose($hBitmap) in your loop. expandcollapse popup#include <GDIPlus.au3> #include <ScreenCapture.au3> #include <WinAPIHObj.au3> Local $handle = WinGetHandle("[CLASS:SciTEWindow]", "") Local $iRegion_X = 10, $iRegion_Y = 10, $iRegion_W = 100, $iRegion_H = 100 ; Initialize GDI+ library _GDIPlus_Startup() Local $hBitmap = Capture_Window($handle, _WinAPI_GetWindowWidth($handle), _WinAPI_GetWindowHeight($handle)) _GDIPlus_ImageSaveToFile($hBitmap, @ScriptDir & "\test.bmp") ; Create 24 bit bitmap clone Local $hClone = _GDIPlus_BitmapCloneArea($hBitmap, $iRegion_X, $iRegion_Y, $iRegion_W, $iRegion_H, $GDIP_PXF24RGB) _GDIPlus_ImageSaveToFile($hClone, @ScriptDir & "\GDIPlus_Image.bmp") ; Clean up resources _GDIPlus_ImageDispose($hClone) _GDIPlus_ImageDispose($hBitmap) ; Shut down GDI+ library _GDIPlus_Shutdown() Func Capture_Window($hWnd, $w, $h) If Not IsHWnd($hWnd) Then Return SetError(1, 0, 0) If Int($w) < 1 Then Return SetError(2, 0, 0) If Int($h) < 1 Then Return SetError(3, 0, 0) Local Const $hDC_Capture = _WinAPI_GetDC(HWnd($hWnd)) Local Const $hMemDC = _WinAPI_CreateCompatibleDC($hDC_Capture) Local Const $hHBitmap = _WinAPI_CreateCompatibleBitmap($hDC_Capture, $w, $h) Local Const $hObjectOld = _WinAPI_SelectObject($hMemDC, $hHBitmap) DllCall("gdi32.dll", "int", "SetStretchBltMode", "hwnd", $hDC_Capture, "uint", 4) DllCall("user32.dll", "int", "PrintWindow", "hwnd", $hWnd, "handle", $hMemDC, "int", 0) _WinAPI_DeleteDC($hMemDC) _WinAPI_SelectObject($hMemDC, $hObjectOld) _WinAPI_ReleaseDC($hWnd, $hDC_Capture) Local Const $hFullScreen = WinGetHandle("[TITLE:Program Manager;CLASS:Progman]") Local Const $aFullScreen = WinGetPos($hFullScreen) Local Const $c1 = $aFullScreen[2] - @DesktopWidth, $c2 = $aFullScreen[3] - @DesktopHeight Local Const $wc1 = $w - $c1, $hc2 = $h - $c2 If (($wc1 > 1 And $wc1 < $w) Or ($w - @DesktopWidth > 1) Or ($hc2 > 7 And $hc2 < $h) Or ($h - @DesktopHeight > 1)) And (BitAND(WinGetState(HWnd($hWnd)), 32) = 32) Then Local $hBmp_t = _GDIPlus_BitmapCreateFromHBITMAP($hHBitmap) $hBmp = _GDIPlus_BitmapCloneArea($hBmp_t, 8, 8, $w - 16, $h - 16) _GDIPlus_BitmapDispose($hBmp_t) Else $hBmp = _GDIPlus_BitmapCreateFromHBITMAP($hHBitmap) EndIf _WinAPI_DeleteObject($hHBitmap) Return $hBmp EndFunc ;==>Capture_Window Edited December 19, 2019 by KaFu memerim 1 OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2024-Oct-13) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Oct-13) HMW - Hide my Windows (2024-Oct-19) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2024-Oct-20) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
memerim Posted December 19, 2019 Author Share Posted December 19, 2019 Thank you again Kafu, but instead cropping a area of the clone bmp, is possible the Capture_Window function save the screenshot of a custom area? Im asking because im looping this function, and its generating a 4mb file in each second, its 14gb of 'useless' data per hour just to find a bmp. Link to comment Share on other sites More sharing options...
KaFu Posted December 19, 2019 Share Posted December 19, 2019 Just comment the _GDIPlus_ImageSaveToFile($hBitmap, @ScriptDir & "\test.bmp") line out? memerim 1 OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2024-Oct-13) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Oct-13) HMW - Hide my Windows (2024-Oct-19) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2024-Oct-20) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
memerim Posted December 19, 2019 Author Share Posted December 19, 2019 Omg i felt very dumb now 😝🤪 Link to comment Share on other sites More sharing options...
KaFu Posted December 19, 2019 Share Posted December 19, 2019 Don't, I started re-writing the function before I realized it myself 😉 OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2024-Oct-13) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Oct-13) HMW - Hide my Windows (2024-Oct-19) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2024-Oct-20) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) 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