Jump to content

Recommended Posts

Posted

I am trying to take a screenshot of a window that is not in the foreground.

I read the help file and I searched the forum. But somehow I just can't get it to work...

I tried multiple versions. Noone work. They simply capture my current screen OR capture a small black rectangle.

#include <ScreenCapture.au3>

Main()

Func Main()
   Local $handle
   $handle = WinGetHandle("Untitled - Notepad");
   ; Capture window
   _ScreenCapture_CaptureWnd(@MyDocumentsDir & "\test.jpg", $handle);

   ShellExecute(@MyDocumentsDir & "\test.jpg")

EndFunc
#include <ScreenCapture.au3>

Main()

Func Main()
   Local $handle
   $handle = 0x00AC05BA
   ; Capture window
   _ScreenCapture_CaptureWnd(@MyDocumentsDir & "\test.jpg", $handle);

   ShellExecute(@MyDocumentsDir & "\test.jpg")

EndFunc

Any Ideas why this is not working? I am trying to build a browser based streaming service. So it would be great if I could capture windows that are not in the front.

XXGTkLr.png

Posted

If you are using Vista / Server 2008 try this:

#include <ScreenCapture.au3>

Main()

Func Main()
   Local $handle
   $handle = WinGetHandle("Untitled - Notepad");
   ; Capture window
   _GDIPlus_Startup("gdiplus.dll")
   _ScreenCapture_CaptureWnd(@MyDocumentsDir & "\test.jpg", $handle);
   _GDIPlus_Shutdown()

   ShellExecute(@MyDocumentsDir & "\test.jpg")

EndFunc

Br,

UEZ

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Posted

Thanks for the help :)

I am on Windows 8.1

But your example did get me slightly further. The screenshot now takes an image of whatever is at the position of the notepad window.  So basically like a _ScreenCapture_Capture with set position.

Posted

This example is working on WIN_7/Service Pack 1.

#include <ScreenCapture.au3>
#include <WinAPIConstants.au3>
Opt("WinTitleMatchMode", -2) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase

_ScreenCaptureWnd(" - notepad")


Func _ScreenCaptureWnd($sWinTitle)
    Local $iMin = BitAND(WinGetState($sWinTitle), 16) ; Check if $sWinTitle window is minimized.
    ;ConsoleWrite($iMin & @LF)

    If $iMin <> 16 Then ; If not minimized
        Local $aWinList = WinList()
        For $i = 1 To UBound($aWinList) - 1
            If BitAND($aWinList[$i][1], 8) Then ; Get the top-most (active) window.
                $hTopMost = $aWinList[$i][0]
                ExitLoop
            EndIf
        Next
    Else ; If minimized
        WinSetState($sWinTitle, "", @SW_SHOW)
    EndIf

    WinActivate($sWinTitle)
    WinWaitActive($sWinTitle)
    Local $aWinPos = WinGetPos($sWinTitle) ; Get original position of $sWinTitle window.
    WinMove($sWinTitle, "", 0, 0) ; In case $sWinTitle window is partly off desktop area.

    _ScreenCapture_CaptureWnd(@MyDocumentsDir & "\test.jpg", WinGetHandle($sWinTitle))

    WinMove($sWinTitle, "", $aWinPos[0], $aWinPos[1]) ; Re-position $sWinTitle window.

    If $iMin <> 16 Then ; If not minimized
        WinActivate($hTopMost)
        WinWaitActive($hTopMost) ; Re-establish original top-most (active) window.
    Else ; If minimized
        WinSetState($sWinTitle, "", @SW_MINIMIZE)
    EndIf
    ShellExecute(@MyDocumentsDir & "\test.jpg")
EndFunc   ;==>_ScreenCaptureWnd
Posted

Thanks Malkey :)

It works.

But sadly it relies on putting the window in the front. I was hoping to take a screenshot in the background. Similar to streaming video.

I guess I look into Python and other options :)

Posted

Then try this:

#include <GDIPlus.au3>

_GDIPlus_Startup()
Global $handle = WinGetHandle("[CLASS:Notepad]")
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

The captured window might be looking different than the original window. This is a problem with the PrintWindow function!

Br,

UEZ

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

  • 10 months later...
Posted

Apologies for refreshing the topic, but with windows 7 x64 ,  sometimes it will capture the screen successfully, and sometimes it's just a black screen.  However when i test it with a static window like mspaint, it is 100%.   Is there anything i can do to ensure the non-static window can capture it successfully? for example freeze the window during a capture perhaps?  Or is there anything i can look into to test around? Thanks.

Posted

On same window or on different windows? If different windows then what kind of appl. are you trying? Video? Games?

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

  • 2 weeks later...
Posted (edited)

sorry for getting back late.  i would be lying if i said it wasn't for a game.  Yes, i know it's against the rules.  but if you wanted to know it's for bluestacks, however it's more for the reward of solving a problem.  but thanks for what you posted it could be useful someday. 

Edited by tntaien
  • Developers
Posted (edited)

sorry for getting back late.  i would be lying if i said it wasn't for a game.  Yes, i know it's against the rules.  but if you wanted to know it's for bluestacks, however it's more for the reward of solving a problem.  but thanks for what you posted it could be useful someday.  

​You better think before posting one more post on this topic. Our rules are crystal clear and since you knew already you are posting on a topic hat isn't allowed, you've use up all your credits.

Jos
 

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

  • Jos locked this topic
Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...