Jump to content

Screen Capture sometimes get blank webpage


Recommended Posts

hi brothers, 

I'm trying to take screenshot from webpage and if found code that could take screenshot even when window is minimized but sometimes i get image like Image Attached I'm trying for 4 hours ago to know where is the problem ...

Notice: for some reason I'm need to run site minimized :)

 

ScreenShot CaptureTest.jpg

The Code:
 

;coded by UEZ 2012
#include <Array.au3>
#include <Constants.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <IE.au3>
Opt("WinTitleMatchMode", 2)
    Local $oIE = _IECreate("https://mybanner.qu.edu.qa/PROD/twbkwbis.P_WWWLogin")
         opt('winwaitdelay', 0)  ;to make it run a little bit faster    
            WinWait("User Login")
                $handle = WinMove("User Login - Internet Explorer","",0,0,877, 516)
            WinSetState("User Login","",@SW_SHOWMINIMIZED)
        _IELoadWait($oIE)
        Sleep(2000)

WinSetState("User Login","",@SW_SHOWDEFAULT)

$aWinlist = GetAllWindow()
$handle = WinMove("Student Employment Time Attendance","",0,0,877, 516)
$MyWindow=WinGetHandle("[Class:IEFrame]")
ConsoleWrite($MyWindow)
For $i = 0 To UBound($aWinlist) - 1
    $hWnd = $aWinlist[$i][1]

    If Not WinActive($hWnd) Then ExitLoop
Next
If $i = UBound($aWinlist) Then Exit MsgBox(0, "Info", "No inactive window found", 10)

_GDIPlus_Startup()
$hBitmap = Capture_Window($MyWindow, 877, 516)
_GDIPlus_ImageSaveToFile($hBitmap, @ScriptDir & "Test.jpg")
_GDIPlus_BitmapDispose($hBitmap)
ShellExecute(@ScriptDir & "Test.jpg")
_GDIPlus_Shutdown()
Exit

Func Capture_Window($hWnd, $w, $h)
    Local $hDC_Capture = _WinAPI_GetWindowDC($hWnd)
    Local $hMemDC = _WinAPI_CreateCompatibleDC($hDC_Capture)
    Local $hHBitmap = _WinAPI_CreateCompatibleBitmap($hDC_Capture, $w, $h)
    Local $hObject = _WinAPI_SelectObject($hMemDC, $hHBitmap)
    DllCall("user32.dll", "int", "PrintWindow", "hwnd", $hWnd, "handle", $hMemDC, "int", 0)
    _WinAPI_DeleteDC($hMemDC)
;    Local $hObject = _WinAPI_SelectObject($hMemDC, $hObject)
    _WinAPI_ReleaseDC($hWnd, $hDC_Capture)
    Local $hBmp = _GDIPlus_BitmapCreateFromHBITMAP($hHBitmap)
    _WinAPI_DeleteObject($hHBitmap)
    Return $hBmp
EndFunc   ;==>Capture_Window

Func GetAllWindow() ;code by Authenticity - modified by UEZ
    Local $aWin = WinList(), $aWindows[1][4]
    Local $iStyle, $iEx_Style, $iCounter = 0
    Local $i, $hWnd_state, $aWinPos

    For $i = 1 To $aWin[0][0]
        $iEx_Style = BitAND(_WinAPI_GetWindowLong($aWin[$i][1], $GWL_EXSTYLE), $WS_EX_TOOLWINDOW)
        $iStyle = BitAND(WinGetState($aWin[$i][1]), 2)
        If $iEx_Style <> -1 And Not $iEx_Style And $iStyle Then
            $aWinPos = WinGetPos($aWin[$i][1])
            If $aWinPos[2] > 1 And $aWinPos[3] > 1 Then
                $aWindows[$iCounter][0] = $aWin[$i][0]
                $aWindows[$iCounter][1] = $aWin[$i][1]
                $aWindows[$iCounter][2] = $aWinPos[2]
                $aWindows[$iCounter][3] = $aWinPos[3]
                $iCounter += 1
            EndIf
            ReDim $aWindows[$iCounter + 1][4]
        EndIf
    Next
    ReDim $aWindows[$iCounter][4]
    Return $aWindows
EndFunc   ;==>GetAllWindow

 

Edited by abdulrahmanok
Forgot to add code..
Link to comment
Share on other sites

  • Moderators

@abdulrahmanok you have been around long enough to know better than, and have in fact been warned about, bumping your posts without waiting 24 hours. This may be the most important thing in the world to you, but you need to show some patience.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

What if you just tried using the built-in window capture UDF instead of an external function?

#include <ScreenCapture.au3>

_GDIPlus_Startup()
;$hBitmap = Capture_Window($MyWindow, 877, 516)
$hBitmap = _ScreenCapture_CaptureWnd(@ScriptDir & "\Test.jpg", $myWindow) ;<=======ADDED this line
;_GDIPlus_ImageSaveToFile($hBitmap, @ScriptDir & "Test.jpg")
_GDIPlus_BitmapDispose($hBitmap)
ShellExecute(@ScriptDir & "Test.jpg")
_GDIPlus_Shutdown()

If you want to tweak the bounds you can do so with extra parameters after the window handle.

UHJvZmVzc2lvbmFsIENvbXB1dGVyZXI=

Link to comment
Share on other sites

  • Developers
4 hours ago, abdulrahmanok said:

_GDIPlus_ImageSaveToFile($hBitmap, @ScriptDir & "Test.jpg")

Looks like you are missing a slash (/) in that line.

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.
  :)

Link to comment
Share on other sites

Try this:

;coded by UEZ 2012
#include <Array.au3>
#include <Constants.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <IE.au3>
Opt("WinTitleMatchMode", 2)
    Local $oIE = _IECreate("https://mybanner.qu.edu.qa/PROD/twbkwbis.P_WWWLogin")
         opt('winwaitdelay', 0)  ;to make it run a little bit faster
            WinWait("User Login")
                $handle = WinMove("User Login - Internet Explorer","",0,0,877, 516)
            WinSetState("User Login","",@SW_SHOWMINIMIZED)
        _IELoadWait($oIE)
        Sleep(2000)

WinSetState("User Login","",@SW_SHOWDEFAULT)

$aWinlist = GetAllWindow()
$handle = WinMove("Student Employment Time Attendance","",0,0,877, 516)
$MyWindow=WinGetHandle("[Class:IEFrame]")
ConsoleWrite($MyWindow)
For $i = 0 To UBound($aWinlist) - 1
    $hWnd = $aWinlist[$i][1]
    If Not WinActive($hWnd) Then ExitLoop
Next
If $i = UBound($aWinlist) Then Exit MsgBox(0, "Info", "No inactive window found", 10)
Sleep(1000)
_GDIPlus_Startup()
$hBitmap = Capture_Window($MyWindow, 877, 516)
_GDIPlus_ImageSaveToFile($hBitmap, @ScriptDir & "\Test.jpg")
_GDIPlus_BitmapDispose($hBitmap)
ShellExecute(@ScriptDir & "\Test.jpg")
_GDIPlus_Shutdown()
Exit

Func Capture_Window($hWnd, $w, $h)
    Local $hDC_Capture = _WinAPI_GetWindowDC($hWnd)
    Local $hMemDC = _WinAPI_CreateCompatibleDC($hDC_Capture)
    Local $hHBitmap = _WinAPI_CreateCompatibleBitmap($hDC_Capture, $w, $h)
    Local $hObject = _WinAPI_SelectObject($hMemDC, $hHBitmap)
    DllCall("user32.dll", "int", "PrintWindow", "hwnd", $hWnd, "handle", $hMemDC, "int", 0)
    _WinAPI_DeleteDC($hMemDC)
;    Local $hObject = _WinAPI_SelectObject($hMemDC, $hObject)
    _WinAPI_ReleaseDC($hWnd, $hDC_Capture)
    Local $hBmp = _GDIPlus_BitmapCreateFromHBITMAP($hHBitmap)
    _WinAPI_DeleteObject($hHBitmap)
    Return $hBmp
EndFunc   ;==>Capture_Window

Func GetAllWindow() ;code by Authenticity - modified by UEZ
    Local $aWin = WinList(), $aWindows[1][4]
    Local $iStyle, $iEx_Style, $iCounter = 0
    Local $i, $hWnd_state, $aWinPos

    For $i = 1 To $aWin[0][0]
        $iEx_Style = BitAND(_WinAPI_GetWindowLong($aWin[$i][1], $GWL_EXSTYLE), $WS_EX_TOOLWINDOW)
        $iStyle = BitAND(WinGetState($aWin[$i][1]), 2)
        If $iEx_Style <> -1 And Not $iEx_Style And $iStyle Then
            $aWinPos = WinGetPos($aWin[$i][1])
            If $aWinPos[2] > 1 And $aWinPos[3] > 1 Then
                $aWindows[$iCounter][0] = $aWin[$i][0]
                $aWindows[$iCounter][1] = $aWin[$i][1]
                $aWindows[$iCounter][2] = $aWinPos[2]
                $aWindows[$iCounter][3] = $aWinPos[3]
                $iCounter += 1
            EndIf
            ReDim $aWindows[$iCounter + 1][4]
        EndIf
    Next
    ReDim $aWindows[$iCounter][4]
    Return $aWindows
EndFunc   ;==>GetAllWindow

 

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 comment
Share on other sites

I assume you can do a save as html/pdf with IE.AU3 library. So print to a file
https://msdn.microsoft.com/en-us/library/ms970457.aspx

$oIE.ExecWB did do this in the past not used it for a long time (google is your friend)

https://win32com.goermezer.de/microsoft/internet-explorer/printing-webpages-from-internet-explorer-with-python.html

  • dont make it minimized just make it 1 * 1 pixel or 10 * 10 and move it behind your startbar
Edited by junkew
dont make it minimized just make it 1 * 1 pixel or 10 * 10
Link to comment
Share on other sites

Thanks for your reply but I have no experiance with c++ codes and I couldn't find  $oIE.ExecWB Autoit code expamles  (after google it)
whatever I searched also about "autoit save page as pdf" but most of codes doesn't work or it doesn't do what is want.

Link to comment
Share on other sites

@UEZ Thank you for mention to this great way but unfortunately after many tries I known that this way can run by load ULR in separate GUI and then capture it  and in my case it will fails to get page when I use site need to login for example capture some pages after already login to my college page or take screen shot of Autoit account details page.

Result of current captured page:
 

Test1.png

Link to comment
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
 Share

×
×
  • Create New...