Jump to content
Sign in to follow this  
Gui

Possible to Screen Capture while GUI is hidden?

Recommended Posts

Gui

Aye guys, just want to know if it's possible to screen capture something, like an image, while the GUI is hidden or in the tray. If it is, how would you do it? I currently have to show my GUI first, ( if it's minimized ), then screen capture to make sure I get the image I want.

Like :

$hwnd = WinGetHandle( 'Get Your Stats!')
$filename = @DesktopDir & '\Stats.jpg'
$oImg2 = _IEImgGetCollection( $oIE, 5)
        $right = $oImg2.getBoundingClientRect().right
    $left = $oImg2.getBoundingClientRect().left
    $top = $oImg2.getBoundingClientRect().top
    $bottom = $oImg2.getBoundingClientRect().bottom
$iI = _ScreenCapture_CaptureWnd( $filename, $hwnd, $left, $top, $right, $bottom, False)
_ScreenCapture_SaveImage( $filename, $iI)
    GUICtrlCreatePic( $filename, 1, 1, 240, 60)

Share this post


Link to post
Share on other sites
picea892

unfortunately, I think you have to restore the window before screencapturing. I have been told you can hide the window, restore it and screencapture the hidden window then minimize it again.

Share this post


Link to post
Share on other sites
Gui

unfortunately, I think you have to restore the window before screencapturing. I have been told you can hide the window, restore it and screencapture the hidden window then minimize it again.

Meh, thanks anyway :D

Share this post


Link to post
Share on other sites
James

(Try again, post before broke).

#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <ScreenCapture.au3>

_GDIPlus_Startup()

$hGUI = WinGetHandle("AutoIt Help")
WinMove($hGUI, "", @DesktopWidth-100, @DesktopHeight)

$iWidth = _WinAPI_GetWindowWidth($hGUI)
$iHeight = _WinAPI_GetWindowHeight($hGUI)

$hParent = GUICreate("WindowViewer", 500, 400)
$hGraphics = _GDIPlus_GraphicsCreateFromHWND($hParent)
GUISetState()

While 1
    $hBMP = _WinCapture($hGUI, $iWidth, $iHeight)
    $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBMP)

    _GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hImage, 0, 0, $iWidth, $iHeight, 50, 50, 400, 300)
    _GDIPlus_ImageDispose($hImage)
    _WinAPI_DeleteObject($hBMP)

    Sleep(1000)
WEnd

Func _WinCapture($hWnd, $iWidth = -1, $iHeight = -1)
    Local $iH, $iW, $hDDC, $hCDC, $hBMP

    If $iWidth = -1 Then $iWidth = _WinAPI_GetWindowWidth($hWnd)
    If $iHeight = -1 Then $iHeight = _WinAPI_GetWindowHeight($hWnd)

    $hDDC = _WinAPI_GetDC($hWnd)
    $hCDC = _WinAPI_CreateCompatibleDC($hDDC)
    $hBMP = _WinAPI_CreateCompatibleBitmap($hDDC, $iWidth, $iHeight)
    _WinAPI_SelectObject($hCDC, $hBMP)

    DllCall("User32.dll", "int", "PrintWindow", "hwnd", $hWnd, "hwnd", $hCDC, "int", 0)
    _WinAPI_BitBlt($hCDC, 0, 0, $iW, $iH, $hDDC, 0, 0, 0x00330008)

    _WinAPI_ReleaseDC($hWnd, $hDDC)
    _WinAPI_DeleteDC($hCDC)

    _ScreenCapture_SaveImage(@DesktopDir&"\window.jpg", $hBMP)
    _WinAPI_DeleteObject($hBMP)

    Return $hBMP
EndFunc   ;==>_WinCapture

Share this post


Link to post
Share on other sites
wraithdu

That example is still a little broken. Here:

#NoTrayIcon
#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <ScreenCapture.au3>

_GDIPlus_Startup()

$hGUI = WinGetHandle("AutoIt Help")
WinMove($hGUI, "", @DesktopWidth-100, @DesktopHeight)

$iWidth = _WinAPI_GetWindowWidth($hGUI)
$iHeight = _WinAPI_GetWindowHeight($hGUI)

$hParent = GUICreate("WindowViewer", 500, 400)
$hGraphics = _GDIPlus_GraphicsCreateFromHWND($hParent)
GUISetState()

$hBMP = _WinCapture($hGUI, $iWidth, $iHeight)
$hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBMP)

_GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hImage, 0, 0, $iWidth, $iHeight, 50, 50, 400, 300)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_GraphicsDispose($hGraphics)
_WinAPI_DeleteObject($hBMP)
Sleep(5000)

Func _WinCapture($hWnd, $iWidth = -1, $iHeight = -1)
    Local $iH, $iW, $hDDC, $hCDC, $hBMP

    If $iWidth = -1 Then $iWidth = _WinAPI_GetWindowWidth($hWnd)
    If $iHeight = -1 Then $iHeight = _WinAPI_GetWindowHeight($hWnd)

    $hDDC = _WinAPI_GetDC($hWnd)
    $hCDC = _WinAPI_CreateCompatibleDC($hDDC)
    $hBMP = _WinAPI_CreateCompatibleBitmap($hDDC, $iWidth, $iHeight)
    _WinAPI_SelectObject($hCDC, $hBMP)

    DllCall("User32.dll", "int", "PrintWindow", "hwnd", $hWnd, "hwnd", $hCDC, "int", 0)
    _WinAPI_BitBlt($hCDC, 0, 0, $iW, $iH, $hDDC, 0, 0, 0x00330008)

    _WinAPI_ReleaseDC($hWnd, $hDDC)
    _WinAPI_DeleteDC($hCDC)

    _ScreenCapture_SaveImage(@DesktopDir&"\window.jpg", $hBMP, False)

    Return $hBMP
EndFunc   ;==>_WinCapture

Share this post


Link to post
Share on other sites
picea892

Are you running vista or windows 7?

This may be an option

Share this post


Link to post
Share on other sites
Gui

Thanks guys for all the replys. I've will try yours @ James as soon as I can. Also

I run on Windows 7.

Share this post


Link to post
Share on other sites
wraithdu

Really? It worked for me.

Yeah, Win7 32-bit.

The problem in your version was that the $hBMP object was disposed of (twice, actually) before drawing to the viewer GUI. Once implicitly by the _ScreenCapture_SaveImage function (to fix set the last parameter to False), then again explicitly by your call to _WinAPI_DeleteObject (both in the _WinCapture function). I didn't see the point of the main loop either.

Share this post


Link to post
Share on other sites
James

Yeah, Win7 32-bit.

The problem in your version was that the $hBMP object was disposed of (twice, actually) before drawing to the viewer GUI. Once implicitly by the _ScreenCapture_SaveImage function (to fix set the last parameter to False), then again explicitly by your call to _WinAPI_DeleteObject (both in the _WinCapture function). I didn't see the point of the main loop either.

It did however work.

Either way, it wasn't my example, just something I collected from the forum. Now you've pointed it out, I see it's completely redundant.

Share this post


Link to post
Share on other sites
Ash

Hi peeps,

this is an old script from Toady, which I use constantly for doco.

I recompiled it using current Screen capture, GDI and WinApi UDF's.

Prompts for a file name, can save in four image types, has a viewer for existing files.

Allows partial screen capture using rubber banding.

Uses the S key to select top-left and bottom-right which allows for window management before the capture is taken.

Hope you find as much use for it as I do!

regards,

Ash

Share this post


Link to post
Share on other sites
Ash

Well, that file attachment went into the ether, so heres the code ....

;==============================================
; Author: Toady
;
; Purpose: Takes screenshot of
; a user selected region and saves
; the screenshot in ./images directory.
;
; Rewrite by Ashaman42 for latest UDF's
;
; How to use:
; Press "s" key to select region corners.
; NOTE: Must select top-left of region
; first, then select bottom-right of region.
;=============================================

#include <ScreenCapture.au3>
#include <misc.au3>
#include <GuiConstants.au3>
#include <WinAPI.au3>
#Include <GuiListBox.au3>
#Include <GuiStatusBar.au3>
#include <GDIPlus.au3>
#include <WindowsConstants.au3>

_Singleton("cap")

Global $format = ".jpg"
Global $filename = ""
Global $title = "Screen Region Capture"

DirCreate(@ScriptDir & "/images/")

$GUI = GUICreate($title,610,210,-1,-1)
GUICtrlCreateGroup("Select Format",18,80,210,50)
$radio1 = GUICtrlCreateRadio("JPG",30,100,40)
GuiCtrlSetState(-1, $GUI_CHECKED)
$radio2 = GUICtrlCreateRadio("BMP",80,100,45)
$radio3 = GUICtrlCreateRadio("GIF",130,100,45)
$radio4 = GUICtrlCreateRadio("PNG",180,100,45)
GUICtrlCreateGroup ("",-99,-99,1,1)
GUICtrlCreateLabel("Name of image",20,20)
$gui_img_input = GUICtrlCreateInput("",20,40,200,20)
$go_button = GUICtrlCreateButton("Select region",20,140,200,30)
$list = GUICtrlCreateList("",240,20,150,150)
$editbutton = GUICtrlCreateButton("Edit", 400,60,40)
$deletebutton = GUICtrlCreateButton("Delete", 400,100,40)
$exitbutton = GUICtrlCreateButton("Exit", 400,140,40)
Global $a_PartsRightEdge[5] = [240,320,400,480,-1]
Global $a_PartsText[5] = [@TAB & "Your Name Here!","","","",""]
Global $hImage
$statusbar = _GUICtrlStatusBar_Create($GUI,$a_PartsRightEdge,$a_PartsText)
GUISetState(@SW_SHOW,$GUI)

$SELECT_H =  GUICreate( "AU3SelectionBox", 0 , 0 , 0, 0,  $WS_POPUP + $WS_BORDER, $WS_EX_TOPMOST, $WS_EX_TOOLWINDOW)
GUISetBkColor(0x00FFFF,$SELECT_H)
WinSetTrans("AU3SelectionBox","",60)
GUISetState(@SW_SHOW,$SELECT_H)

_ListFiles()

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case $msg = $GUI_EVENT_RESTORE
            _ListFiles()
        Case $msg = $radio1
            $format = ".jpg"
        Case $msg = $radio2
            $format = ".bmp"
        Case $msg = $radio3
            $format = ".gif"
        Case $msg = $radio4
            $format = ".png"
        Case $msg = $editbutton
            ShellExecute(_GUICtrlListBox_GetText($list,_GUICtrlListBox_GetCurSel($list)),"",@ScriptDir & "\images\","edit")
        Case $msg = $deletebutton
            Local $msgbox = MsgBox(4,"Delete image","Are you sure?")
            If $msgbox = 6 Then
                FileDelete(@ScriptDir & "\images\" & _GUICtrlListBox_GetText($list,_GUICtrlListBox_GetCurSel($list)))
                _ListFiles()
            EndIf
        Case $msg = $exitbutton
            Exit
        Case $msg = $list
            _ListClick()
        Case $msg = $go_button
            $filename = GUICtrlRead($gui_img_input)
            If $filename <> "" Then
                Local $msgbox = 6
                If FileExists(@ScriptDir & "\images\" & $filename & $format) Then
                    $msgbox = MsgBox(4,"Already Exists","File name already exists, do you want to overwrite it?")
                EndIf
                If $msgbox = 6 Then
                    GUISetState(@SW_HIDE,$GUI)
                    _WinAPI_MoveWindow($SELECT_H,1,1,2,2)
                    GUISetState(@SW_RESTORE,$SELECT_H)
                    GUISetState(@SW_SHOW,$SELECT_H)
                    _TakeScreenShot()
                    GUICtrlSetData($gui_img_input,"")
                    GUISetState(@SW_SHOW,$GUI)
                    _ListFiles()
                EndIf
            Else
                MsgBox(0,"Error","Enter a filename")
            EndIf
    EndSelect
WEnd

Func _ConvertSize($size_bytes)
    If $size_bytes < 1024*1024 Then
        Return Round($size_bytes/1024,2) & " KB"
    Else
        Return Round($size_bytes/1024/1024,2) & " MB"
    EndIf
EndFunc

Func _ConvertTime($time)
    Local $time_converted = $time
    Local $time_data = StringSplit($time, ":")
    If $time_data[1] > 12 Then
        $time_converted = $time_data[1] - 12 & ":" & $time_data[2] & " PM"
    ElseIf $time_data[1] = 12 Then
        $time_converted = $time & " PM"
    Else
        $time_converted &= " AM"
    EndIf
    Return $time_converted
EndFunc

Func _ListClick()
    If _GUICtrlListBox_GetCurSel($list) = -1 Then ;List clicked but nothing selected
        Return
    EndIf
    GUICtrlSetState($deletebutton, $GUI_ENABLE)
    GUICtrlSetState($editbutton, $GUI_ENABLE)
    Local $date = FileGetTime(@ScriptDir & "\images\" & _GUICtrlListBox_GetText($list,_GUICtrlListBox_GetCurSel($list)))
    _GUICtrlStatusBar_SetText($statusbar,@tab & "Size: " & _ConvertSize(FileGetSize(@ScriptDir & "\images\" & _GUICtrlListBox_GetText($list,_GUICtrlListBox_GetCurSel($list)))),1)
    _GUICtrlStatusBar_SetText($statusbar,@tab & "Date: " & $date[1] & "/" & $date[2] & "/" & StringTrimLeft($date[0],2) & " " & _ConvertTime($date[3] & ":" & $date[4]),4)
    _GDIPlus_StartUp()
    $hImage  = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\images\" & _GUICtrlListBox_GetText($list,_GUICtrlListBox_GetCurSel($list)))
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($gui)
    $iWidth  = _GDIPlus_ImageGetWidth ($hImage)
    $iHeight = _GDIPlus_ImageGetHeight($hImage)
    _GUICtrlStatusBar_SetText($statusbar,@tab & "Width: " & $iWidth,2)
    _GUICtrlStatusBar_SetText($statusbar,@tab & "Height: " & $iHeight,3)
    Local $destW = 150
    Local $destH = 150
    _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage, 0, 0,$iWidth,$iHeight,450,20,$destW ,$destH)
    _GDIPlus_GraphicsSetSmoothingMode($hGraphic, 0)
    _GDIPlus_GraphicsDrawRect($hGraphic, 450, 20, $destW, $destH)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_ShutDown()
EndFunc

Func _TakeScreenShot()
    Local $x, $y
    HotKeySet("s","_DoNothing")
    While Not _IsPressed(Hex(83,2))
        Local $currCoord = MouseGetPos()
        Sleep(10)
        ToolTip("Select top-left coord with 's' key" & @CRLF & "First coord: " & $currCoord[0] & "," & $currCoord[1])
        If _IsPressed(Hex(83,2)) Then
            While _IsPressed(Hex(83,2))
                Sleep(10)
            WEnd
            ExitLoop 1
        EndIf
    WEnd
    Local $firstCoord = MouseGetPos()
    _WinAPI_MoveWindow($SELECT_H,$firstCoord[0],$firstCoord[1],1,1)
    While Not _IsPressed(Hex(83,2))
        Local $currCoord = MouseGetPos()
        Sleep(10)
        ToolTip("Select bottom-right coord with 's' key" & @CRLF & "First coord: " & $firstCoord[0] & "," & $firstCoord[1] _
        & @CRLF & "Second coord: " & $currCoord[0] & "," & $currCoord[1] & @CRLF & "Image size: " & _
        Abs($currCoord[0]-$firstCoord[0]) & "x" & Abs($currCoord[1]-$firstCoord[1]))
        $x = _RubberBand_Select_Order($firstCoord[0],$currCoord[0])
        $y = _RubberBand_Select_Order($firstCoord[1],$currCoord[1])
        _WinAPI_MoveWindow($SELECT_H,$x[0],$y[0],$x[1],$y[1])
        If _IsPressed(Hex(83,2)) Then
            While _IsPressed(Hex(83,2))
                Sleep(10)
            WEnd
            ExitLoop 1
        EndIf
    WEnd
    ToolTip("")
    Local $secondCoord = MouseGetPos()
    _WinAPI_MoveWindow($SELECT_H,1,1,2,2)
    GUISetState(@SW_HIDE,$SELECT_H)
    Sleep(100)
    _ScreenCapture_SetJPGQuality(80)
    _ScreenCapture_Capture(@ScriptDir & "\images\" & $filename & $format,$x[0], $y[0], $x[1]+$x[0], $y[1]+$y[0])
    HotKeySet("s")
EndFunc

Func _ListFiles()
    _GUICtrlListBox_ResetContent($list)
    $search = FileFindFirstFile(@ScriptDir & "\images\*.*")
    If $search <> -1 Then
        While 1
            Local $file = FileFindNextFile($search)
            If @error Then ExitLoop
            If StringRegExp($file,"(.bmp)|(.jpg)|(.gif)|(.png)") Then
                $test_error = _GUICtrlListBox_AddFile($list, @ScriptDir & "\images\" & $file)
            EndIf
        WEnd
    EndIf
    FileClose($search)
    If _GUICtrlListBox_GetCurSel($list) = -1 Then  ; No selection
        GUICtrlSetState($deletebutton, $GUI_DISABLE)
        GUICtrlSetState($editbutton, $GUI_DISABLE)
        _GUICtrlStatusBar_SetText($statusbar,"",1)
        _GUICtrlStatusBar_SetText($statusbar,"",2)
        _GUICtrlStatusBar_SetText($statusbar,"",3)
        _GUICtrlStatusBar_SetText($statusbar,"",4)
        _GDIPlus_Startup()
        $hGraphicss = _GDIPlus_GraphicsCreateFromHWND($GUI)
        _GDIPlus_GraphicsFillRect($hGraphicss, 450, 20, 150, 150)
        _GDIPlus_GraphicsDispose($hGraphicss)
        _GDIPlus_Shutdown()
        _DrawText("Preview",495, 80)
    Else
        GUICtrlSetState($deletebutton, $GUI_ENABLE)
        GUICtrlSetState($editbutton, $GUI_ENABLE)
        _GUICtrlListBox_GetCurSel($list)
        WinWaitActive("Screen Region Capture")
        _ListClick()
    EndIf
EndFunc

Func _DrawText($text,$x, $y)
    _GDIPlus_Startup()
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($GUI)
    $hBrush   = _GDIPlus_BrushCreateSolid(0xFFFFFFFF)
    $hFormat  = _GDIPlus_StringFormatCreate()
    $hFamily  = _GDIPlus_FontFamilyCreate("Arial")
    $hFont    = _GDIPlus_FontCreate($hFamily, 12, 2)
    $tLayout  = _GDIPlus_RectFCreate($x, $y, 0, 0)
    $aInfo    = _GDIPlus_GraphicsMeasureString($hGraphic, $text, $hFont, $tLayout, $hFormat)
    _GDIPlus_GraphicsDrawStringEx($hGraphic, $text, $hFont, $aInfo[0], $hFormat, $hBrush)
    _GDIPlus_FontDispose        ($hFont   )
    _GDIPlus_FontFamilyDispose  ($hFamily )
    _GDIPlus_StringFormatDispose($hFormat )
    _GDIPlus_BrushDispose       ($hBrush  )
    _GDIPlus_GraphicsDispose    ($hGraphic)
    _GDIPlus_Shutdown()
EndFunc

Func _RubberBand_Select_Order($a,$b)
    Dim $res[2]
    If $a < $b Then
        $res[0] = $a
        $res[1] = $b - $a
    Else
        $res[0] = $b
        $res[1] = $a - $b
    EndIf
    Return $res
EndFunc

Func _DoNothing()
EndFunc

hope this second attempt works ....

Ash

Share this post


Link to post
Share on other sites
RoyS

Well, that file attachment went into the ether, so heres the code ....

;==============================================
; Author: Toady
;
; Purpose: Takes screenshot of
; a user selected region and saves
; the screenshot in ./images directory.
;
; Rewrite by Ashaman42 for latest UDF's
;
; How to use:
; Press "s" key to select region corners.
; NOTE: Must select top-left of region
; first, then select bottom-right of region.
;=============================================

#include <ScreenCapture.au3>
#include <misc.au3>
#include <GuiConstants.au3>
#include <WinAPI.au3>
#Include <GuiListBox.au3>
#Include <GuiStatusBar.au3>
#include <GDIPlus.au3>
#include <WindowsConstants.au3>

_Singleton("cap")

Global $format = ".jpg"
Global $filename = ""
Global $title = "Screen Region Capture"

DirCreate(@ScriptDir & "/images/")

$GUI = GUICreate($title,610,210,-1,-1)
GUICtrlCreateGroup("Select Format",18,80,210,50)
$radio1 = GUICtrlCreateRadio("JPG",30,100,40)
GuiCtrlSetState(-1, $GUI_CHECKED)
$radio2 = GUICtrlCreateRadio("BMP",80,100,45)
$radio3 = GUICtrlCreateRadio("GIF",130,100,45)
$radio4 = GUICtrlCreateRadio("PNG",180,100,45)
GUICtrlCreateGroup ("",-99,-99,1,1)
GUICtrlCreateLabel("Name of image",20,20)
$gui_img_input = GUICtrlCreateInput("",20,40,200,20)
$go_button = GUICtrlCreateButton("Select region",20,140,200,30)
$list = GUICtrlCreateList("",240,20,150,150)
$editbutton = GUICtrlCreateButton("Edit", 400,60,40)
$deletebutton = GUICtrlCreateButton("Delete", 400,100,40)
$exitbutton = GUICtrlCreateButton("Exit", 400,140,40)
Global $a_PartsRightEdge[5] = [240,320,400,480,-1]
Global $a_PartsText[5] = [@TAB & "Your Name Here!","","","",""]
Global $hImage
$statusbar = _GUICtrlStatusBar_Create($GUI,$a_PartsRightEdge,$a_PartsText)
GUISetState(@SW_SHOW,$GUI)

$SELECT_H =  GUICreate( "AU3SelectionBox", 0 , 0 , 0, 0,  $WS_POPUP + $WS_BORDER, $WS_EX_TOPMOST, $WS_EX_TOOLWINDOW)
GUISetBkColor(0x00FFFF,$SELECT_H)
WinSetTrans("AU3SelectionBox","",60)
GUISetState(@SW_SHOW,$SELECT_H)

_ListFiles()

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case $msg = $GUI_EVENT_RESTORE
            _ListFiles()
        Case $msg = $radio1
            $format = ".jpg"
        Case $msg = $radio2
            $format = ".bmp"
        Case $msg = $radio3
            $format = ".gif"
        Case $msg = $radio4
            $format = ".png"
        Case $msg = $editbutton
            ShellExecute(_GUICtrlListBox_GetText($list,_GUICtrlListBox_GetCurSel($list)),"",@ScriptDir & "\images\","edit")
        Case $msg = $deletebutton
            Local $msgbox = MsgBox(4,"Delete image","Are you sure?")
            If $msgbox = 6 Then
                FileDelete(@ScriptDir & "\images\" & _GUICtrlListBox_GetText($list,_GUICtrlListBox_GetCurSel($list)))
                _ListFiles()
            EndIf
        Case $msg = $exitbutton
            Exit
        Case $msg = $list
            _ListClick()
        Case $msg = $go_button
            $filename = GUICtrlRead($gui_img_input)
            If $filename <> "" Then
                Local $msgbox = 6
                If FileExists(@ScriptDir & "\images\" & $filename & $format) Then
                    $msgbox = MsgBox(4,"Already Exists","File name already exists, do you want to overwrite it?")
                EndIf
                If $msgbox = 6 Then
                    GUISetState(@SW_HIDE,$GUI)
                    _WinAPI_MoveWindow($SELECT_H,1,1,2,2)
                    GUISetState(@SW_RESTORE,$SELECT_H)
                    GUISetState(@SW_SHOW,$SELECT_H)
                    _TakeScreenShot()
                    GUICtrlSetData($gui_img_input,"")
                    GUISetState(@SW_SHOW,$GUI)
                    _ListFiles()
                EndIf
            Else
                MsgBox(0,"Error","Enter a filename")
            EndIf
    EndSelect
WEnd

Func _ConvertSize($size_bytes)
    If $size_bytes < 1024*1024 Then
        Return Round($size_bytes/1024,2) & " KB"
    Else
        Return Round($size_bytes/1024/1024,2) & " MB"
    EndIf
EndFunc

Func _ConvertTime($time)
    Local $time_converted = $time
    Local $time_data = StringSplit($time, ":")
    If $time_data[1] > 12 Then
        $time_converted = $time_data[1] - 12 & ":" & $time_data[2] & " PM"
    ElseIf $time_data[1] = 12 Then
        $time_converted = $time & " PM"
    Else
        $time_converted &= " AM"
    EndIf
    Return $time_converted
EndFunc

Func _ListClick()
    If _GUICtrlListBox_GetCurSel($list) = -1 Then ;List clicked but nothing selected
        Return
    EndIf
    GUICtrlSetState($deletebutton, $GUI_ENABLE)
    GUICtrlSetState($editbutton, $GUI_ENABLE)
    Local $date = FileGetTime(@ScriptDir & "\images\" & _GUICtrlListBox_GetText($list,_GUICtrlListBox_GetCurSel($list)))
    _GUICtrlStatusBar_SetText($statusbar,@tab & "Size: " & _ConvertSize(FileGetSize(@ScriptDir & "\images\" & _GUICtrlListBox_GetText($list,_GUICtrlListBox_GetCurSel($list)))),1)
    _GUICtrlStatusBar_SetText($statusbar,@tab & "Date: " & $date[1] & "/" & $date[2] & "/" & StringTrimLeft($date[0],2) & " " & _ConvertTime($date[3] & ":" & $date[4]),4)
    _GDIPlus_StartUp()
    $hImage  = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\images\" & _GUICtrlListBox_GetText($list,_GUICtrlListBox_GetCurSel($list)))
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($gui)
    $iWidth  = _GDIPlus_ImageGetWidth ($hImage)
    $iHeight = _GDIPlus_ImageGetHeight($hImage)
    _GUICtrlStatusBar_SetText($statusbar,@tab & "Width: " & $iWidth,2)
    _GUICtrlStatusBar_SetText($statusbar,@tab & "Height: " & $iHeight,3)
    Local $destW = 150
    Local $destH = 150
    _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage, 0, 0,$iWidth,$iHeight,450,20,$destW ,$destH)
    _GDIPlus_GraphicsSetSmoothingMode($hGraphic, 0)
    _GDIPlus_GraphicsDrawRect($hGraphic, 450, 20, $destW, $destH)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_ShutDown()
EndFunc

Func _TakeScreenShot()
    Local $x, $y
    HotKeySet("s","_DoNothing")
    While Not _IsPressed(Hex(83,2))
        Local $currCoord = MouseGetPos()
        Sleep(10)
        ToolTip("Select top-left coord with 's' key" & @CRLF & "First coord: " & $currCoord[0] & "," & $currCoord[1])
        If _IsPressed(Hex(83,2)) Then
            While _IsPressed(Hex(83,2))
                Sleep(10)
            WEnd
            ExitLoop 1
        EndIf
    WEnd
    Local $firstCoord = MouseGetPos()
    _WinAPI_MoveWindow($SELECT_H,$firstCoord[0],$firstCoord[1],1,1)
    While Not _IsPressed(Hex(83,2))
        Local $currCoord = MouseGetPos()
        Sleep(10)
        ToolTip("Select bottom-right coord with 's' key" & @CRLF & "First coord: " & $firstCoord[0] & "," & $firstCoord[1] _
        & @CRLF & "Second coord: " & $currCoord[0] & "," & $currCoord[1] & @CRLF & "Image size: " & _
        Abs($currCoord[0]-$firstCoord[0]) & "x" & Abs($currCoord[1]-$firstCoord[1]))
        $x = _RubberBand_Select_Order($firstCoord[0],$currCoord[0])
        $y = _RubberBand_Select_Order($firstCoord[1],$currCoord[1])
        _WinAPI_MoveWindow($SELECT_H,$x[0],$y[0],$x[1],$y[1])
        If _IsPressed(Hex(83,2)) Then
            While _IsPressed(Hex(83,2))
                Sleep(10)
            WEnd
            ExitLoop 1
        EndIf
    WEnd
    ToolTip("")
    Local $secondCoord = MouseGetPos()
    _WinAPI_MoveWindow($SELECT_H,1,1,2,2)
    GUISetState(@SW_HIDE,$SELECT_H)
    Sleep(100)
    _ScreenCapture_SetJPGQuality(80)
    _ScreenCapture_Capture(@ScriptDir & "\images\" & $filename & $format,$x[0], $y[0], $x[1]+$x[0], $y[1]+$y[0])
    HotKeySet("s")
EndFunc

Func _ListFiles()
    _GUICtrlListBox_ResetContent($list)
    $search = FileFindFirstFile(@ScriptDir & "\images\*.*")
    If $search <> -1 Then
        While 1
            Local $file = FileFindNextFile($search)
            If @error Then ExitLoop
            If StringRegExp($file,"(.bmp)|(.jpg)|(.gif)|(.png)") Then
                $test_error = _GUICtrlListBox_AddFile($list, @ScriptDir & "\images\" & $file)
            EndIf
        WEnd
    EndIf
    FileClose($search)
    If _GUICtrlListBox_GetCurSel($list) = -1 Then  ; No selection
        GUICtrlSetState($deletebutton, $GUI_DISABLE)
        GUICtrlSetState($editbutton, $GUI_DISABLE)
        _GUICtrlStatusBar_SetText($statusbar,"",1)
        _GUICtrlStatusBar_SetText($statusbar,"",2)
        _GUICtrlStatusBar_SetText($statusbar,"",3)
        _GUICtrlStatusBar_SetText($statusbar,"",4)
        _GDIPlus_Startup()
        $hGraphicss = _GDIPlus_GraphicsCreateFromHWND($GUI)
        _GDIPlus_GraphicsFillRect($hGraphicss, 450, 20, 150, 150)
        _GDIPlus_GraphicsDispose($hGraphicss)
        _GDIPlus_Shutdown()
        _DrawText("Preview",495, 80)
    Else
        GUICtrlSetState($deletebutton, $GUI_ENABLE)
        GUICtrlSetState($editbutton, $GUI_ENABLE)
        _GUICtrlListBox_GetCurSel($list)
        WinWaitActive("Screen Region Capture")
        _ListClick()
    EndIf
EndFunc

Func _DrawText($text,$x, $y)
    _GDIPlus_Startup()
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($GUI)
    $hBrush   = _GDIPlus_BrushCreateSolid(0xFFFFFFFF)
    $hFormat  = _GDIPlus_StringFormatCreate()
    $hFamily  = _GDIPlus_FontFamilyCreate("Arial")
    $hFont    = _GDIPlus_FontCreate($hFamily, 12, 2)
    $tLayout  = _GDIPlus_RectFCreate($x, $y, 0, 0)
    $aInfo    = _GDIPlus_GraphicsMeasureString($hGraphic, $text, $hFont, $tLayout, $hFormat)
    _GDIPlus_GraphicsDrawStringEx($hGraphic, $text, $hFont, $aInfo[0], $hFormat, $hBrush)
    _GDIPlus_FontDispose        ($hFont   )
    _GDIPlus_FontFamilyDispose  ($hFamily )
    _GDIPlus_StringFormatDispose($hFormat )
    _GDIPlus_BrushDispose       ($hBrush  )
    _GDIPlus_GraphicsDispose    ($hGraphic)
    _GDIPlus_Shutdown()
EndFunc

Func _RubberBand_Select_Order($a,$b)
    Dim $res[2]
    If $a < $b Then
        $res[0] = $a
        $res[1] = $b - $a
    Else
        $res[0] = $b
        $res[1] = $a - $b
    EndIf
    Return $res
EndFunc

Func _DoNothing()
EndFunc

hope this second attempt works ....

Ash

Share this post


Link to post
Share on other sites
RoyS

the listing for Screen Capture is for me a really great one, have been looking for something that works under AutoIt3 and this is first one found! Thank you!.. Does any one have one that captures a region to the Clipboard so it can be pasted into a Text editor like Word? That is what I really need..

Thanks if you do for any help possible..

Share this post


Link to post
Share on other sites
YellowLab

Here is some code that will capture a window even if it is hidden:

Func _CaptureWindow($hWnd)
    Local $WM_PAINT = 0x000F
    Local $WM_PRINT = 0x317
    Local $PRF_CHILDREN = 0x10; Draw all visible child windows.
    Local $PRF_CLIENT = 0x4 ; Draw the window's client area.
    Local $PRF_OWNED = 0x20 ; Draw all owned windows.
    Local $PRF_NONCLIENT = 0x2 ; Draw the window's Title area.
    Local $PRF_ERASEBKGND = 0x8 ; Erases the background before drawing the window

    Local $pos = WinGetPos($hWnd)
    Local $Width = $pos[2]
    Local $Height = $pos[3]

    Local $hDC = _WinAPI_GetDC($hWnd)
    Local $memDC = _WinAPI_CreateCompatibleDC($hDC)
    Local $memBmp = _WinAPI_CreateCompatibleBitmap($hDC, $Width, $Height)

    _WinAPI_SelectObject ($memDC, $memBmp)

    Local $Ret = _SendMessage($hWnd, $WM_PAINT, $memDC, 0)
    $Ret = _SendMessage($hWnd, $WM_PRINT, $memDC, BitOR($PRF_CHILDREN , $PRF_CLIENT, $PRF_OWNED, $PRF_NONCLIENT, $PRF_ERASEBKGND))

    Local $hBMP=_GDIPlus_BitmapCreateFromHBITMAP($memBmp)
    Local $hHBITMAP=_GDIPlus_BitmapCreateHBITMAPFromBitmap($hBMP)

    _WinAPI_DeleteObject($hDC)
    _WinAPI_ReleaseDC($hWnd, $hDC)
    _WinAPI_DeleteDC($memDC)
    _WinAPI_DeleteObject ($memBmp)
    _WinAPI_DeleteDC($hDC)

    Return $hHBITMAP
EndFunc ;==>_CaptureWindow()

This has been tested and works on WinXP and Win7 64 bit. From here it probably isn't difficult to capture a region of the hidden window.


You can't see a rainbow without first experiencing the rain.

Share this post


Link to post
Share on other sites
Guest

Here is some code that will capture a window even if it is hidden:

Func _CaptureWindow($hWnd)
    Local $WM_PAINT = 0x000F
    Local $WM_PRINT = 0x317
    Local $PRF_CHILDREN = 0x10; Draw all visible child windows.
    Local $PRF_CLIENT = 0x4 ; Draw the window's client area.
    Local $PRF_OWNED = 0x20 ; Draw all owned windows.
    Local $PRF_NONCLIENT = 0x2 ; Draw the window's Title area.
    Local $PRF_ERASEBKGND = 0x8 ; Erases the background before drawing the window

    Local $pos = WinGetPos($hWnd)
    Local $Width = $pos[2]
    Local $Height = $pos[3]

    Local $hDC = _WinAPI_GetDC($hWnd)
    Local $memDC = _WinAPI_CreateCompatibleDC($hDC)
    Local $memBmp = _WinAPI_CreateCompatibleBitmap($hDC, $Width, $Height)

    _WinAPI_SelectObject ($memDC, $memBmp)

    Local $Ret = _SendMessage($hWnd, $WM_PAINT, $memDC, 0)
    $Ret = _SendMessage($hWnd, $WM_PRINT, $memDC, BitOR($PRF_CHILDREN , $PRF_CLIENT, $PRF_OWNED, $PRF_NONCLIENT, $PRF_ERASEBKGND))

    Local $hBMP=_GDIPlus_BitmapCreateFromHBITMAP($memBmp)
    Local $hHBITMAP=_GDIPlus_BitmapCreateHBITMAPFromBitmap($hBMP)

    _WinAPI_DeleteObject($hDC)
    _WinAPI_ReleaseDC($hWnd, $hDC)
    _WinAPI_DeleteDC($memDC)
    _WinAPI_DeleteObject ($memBmp)
    _WinAPI_DeleteDC($hDC)

    Return $hHBITMAP
EndFunc ;==>_CaptureWindow()

This has been tested and works on WinXP and Win7 64 bit. From here it probably isn't difficult to capture a region of the hidden window.

It works almost like a charm, but there is a "little" bug in the code. You forgot to use _GDIPlus_Startup() before any GDIPlus function. Your function then results in zero. And also you have to add these lines at the top in order to make it functional:
#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <SendMessage.au3>

Otherwise, thumbs up!

Share this post


Link to post
Share on other sites
Qwerty212

Thanks for the examples.... but how we can save the captured image into a file. I guess that the image is stored in memory with Return $hHBITMAP But I don't know how to pass it to a bmp file.

Greets from Barcelona

Share this post


Link to post
Share on other sites
johnmcloud

I have try it, but the result is always a black screen ( Win XP SP3 )

#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <SendMessage.au3>
#include <ScreenCapture.au3>

Run(@WindowsDir & "\notepad.exe", "", @SW_MINIMIZE)
Sleep(2000)

_GDIPlus_Startup()
$hHandle = WinGetHandle("[CLASS:Notepad]")
$hBMP =_GDIPlus_BitmapCreateFromHBITMAP(_CaptureWindow($hHandle))
$hHBITMAP =_GDIPlus_BitmapCreateHBITMAPFromBitmap($hBMP)
_ScreenCapture_SaveImage(@WorkingDir & "\Image.bmp", $hHBITMAP, True)
_GDIPlus_Shutdown()

ProcessClose("notepad.exe")

Func _CaptureWindow($hWnd)
    Local $WM_PAINT = 0x000F
    Local $WM_PRINT = 0x317
    Local $PRF_CHILDREN = 0x10; Draw all visible child windows.
    Local $PRF_CLIENT = 0x4 ; Draw the window's client area.
    Local $PRF_OWNED = 0x20 ; Draw all owned windows.
    Local $PRF_NONCLIENT = 0x2 ; Draw the window's Title area.
    Local $PRF_ERASEBKGND = 0x8 ; Erases the background before drawing the window
    Local $pos = WinGetPos($hWnd)
    Local $Width = $pos[2]
    Local $Height = $pos[3]
    Local $hDC = _WinAPI_GetDC($hWnd)
    Local $memDC = _WinAPI_CreateCompatibleDC($hDC)
    Local $memBmp = _WinAPI_CreateCompatibleBitmap($hDC, $Width, $Height)
_WinAPI_SelectObject ($memDC, $memBmp)
Local $Ret = _SendMessage($hWnd, $WM_PAINT, $memDC, 0)
    $Ret = _SendMessage($hWnd, $WM_PRINT, $memDC, BitOR($PRF_CHILDREN , $PRF_CLIENT, $PRF_OWNED, $PRF_NONCLIENT, $PRF_ERASEBKGND))
Local $hBMP=_GDIPlus_BitmapCreateFromHBITMAP($memBmp)
    Local $hHBITMAP=_GDIPlus_BitmapCreateHBITMAPFromBitmap($hBMP)
_WinAPI_DeleteObject($hDC)
    _WinAPI_ReleaseDC($hWnd, $hDC)
    _WinAPI_DeleteDC($memDC)
    _WinAPI_DeleteObject ($memBmp)
    _WinAPI_DeleteDC($hDC)
    Return $hHBITMAP
EndFunc ;==>_CaptureWindow()

Share this post


Link to post
Share on other sites
guinness

Why not get the state of the window, restore it and then reset the state again?


UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Share this post


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
Sign in to follow this  

×