Jump to content

Recommended Posts

 ive had a quick look but couldnt find a direct answer for this, i need to capture an area of a window as an image and save it but my problem is it is very likely that the window i want to capture will have something overlapping it on screen or the user account will be locked and the logon screen will be showing, I know if i use screen capture it will capture the overlapping window or logon screen. Is there a way I can capture an image of the window without capturing something that is displaying on top of it?

Link to post
Share on other sites

Try this _WinAPI_CreateBitmapFromPrintWindow function:

#include <Array.au3>
#include <GDIPlus.au3>
#include <WinAPIGdi.au3>

Global $aWinList = WinList("[REGEXPTITLE:(?i)(.*SciTE.*|.*Internet Explorer.*)]")
If Not IsArray($aWinList) Then Exit
Global $sFile = @ScriptDir & "\Test.jpg"
Global $hBitmap = _WinAPI_CreateBitmapFromPrintWindow($aWinList[Random(1, UBound($aWinList) - 1, 1)][1])
_GDIPlus_ImageSaveToFile($hBitmap, $sFile)

Func _WinAPI_CreateBitmapFromPrintWindow($hWnd, $bCrop = True, $bClient = False, $bBmpGDI = False)
    Local $aWinSize = WinGetPos($hWnd)
    Local Const $hDC_Capture = _WinAPI_GetDC($hWnd)
    Local Const $hMemDC = _WinAPI_CreateCompatibleDC($hDC_Capture)
    Local Const $hHBmp = _WinAPI_CreateCompatibleBitmap($hDC_Capture, $aWinSize[2], $aWinSize[3])
    Local Const $hObjectOld = _WinAPI_SelectObject($hMemDC, $hHBmp)
    _WinAPI_SetStretchBltMode($hDC_Capture, $HALFTONE)
    _WinAPI_PrintWindow($hWnd, $hMemDC, $bClient)
    _WinAPI_SelectObject($hMemDC, $hObjectOld)
    _WinAPI_ReleaseDC($hWnd, $hDC_Capture)
    Local $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hHBmp)
    If $bCrop Then
        Local $hBitmap_Cropped = _GDIPlus_BitmapCloneArea($hBitmap, 8, 8, $aWinSize[2] - 16, $aWinSize[3] - 16)
        $hBitmap = $hBitmap_Cropped
    If $bBmpGDI Then
        Local $hBitmap_GDI = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)
        Return $hBitmap_GDI
    Return $hBitmap

The _WinAPI_PrintWindow function captures the window but not in its original look!

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...