Sign in to follow this  
Followers 0
MrCreatoR

Get screenshot for a client area - Convert Delphi to AutoIt

6 posts in this topic

Hi,

I am trying to get a screen shot for specific area (not coords) in a window. I have peace of code on Delphi, it works very well with it, but i need it on AutoIt :P

Here it is:

function GetScreenShot(const AHandle: THandle; FileName: String; QuickSave: Boolean): Boolean;
var
  fBitmap: PBitmap;
  DC: HDC;
  Rect: TRect;
  OSD: POpenSaveDialog;
  Image: PPngObject;

begin
  Image := NewPngObject;
  fBitmap := nil;
  Result := False;
  DC := GetDC(AHandle);
  if DC <> 0 then
    try
      if not GetClientRect(AHandle, Rect) then Exit;
      fBitmap := NewDIBBitmap(Rect.Right - Rect.Left, Rect.Bottom - Rect.Top, pf32bit);
      Result := BitBlt(fBitmap.Canvas.Handle, 0, 0, fBitmap.Width, fBitmap.Height, DC, 0, 0, SRCCOPY);
      if not Result then Exit;
      Image.AssignHandle(fBitmap.Handle, false, 0);
      if not QuickSave then
        begin
        OSD := NewOpenSaveDialog('', '', DefOpenSaveDlgOptions );
        OSD.Filter := 'PNG - Portable Network Graphics|*.png';
        OSD.Filename := FileName;
        OSD.WndOwner := OpWnd;
        OSD.OpenDialog := false;
        if OSD.Execute then
          begin
          Image.SaveToFile(OSD.Filename);
          end;
        OSD.Free;
        end
      else Image.SaveToFile(FileName);
    finally
      fBitmap.Free;
      Image.Free;
      ReleaseDC(AHandle, DC);
    end;
end;

Can anyone please translate it? The part with OpenSaveDialog is not needed, i need only the screenshot action.


 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites



Hi,

I am trying to get a screen shot for specific area (not coords) in a window. I have peace of code on Delphi, it works very well with it, but i need it on AutoIt :P

Here it is:

function GetScreenShot(const AHandle: THandle; FileName: String; QuickSave: Boolean): Boolean;
var
  fBitmap: PBitmap;
  DC: HDC;
  Rect: TRect;
  OSD: POpenSaveDialog;
  Image: PPngObject;

begin
  Image := NewPngObject;
  fBitmap := nil;
  Result := False;
  DC := GetDC(AHandle);
  if DC <> 0 then
    try
      if not GetClientRect(AHandle, Rect) then Exit;
      fBitmap := NewDIBBitmap(Rect.Right - Rect.Left, Rect.Bottom - Rect.Top, pf32bit);
      Result := BitBlt(fBitmap.Canvas.Handle, 0, 0, fBitmap.Width, fBitmap.Height, DC, 0, 0, SRCCOPY);
      if not Result then Exit;
      Image.AssignHandle(fBitmap.Handle, false, 0);
      if not QuickSave then
        begin
        OSD := NewOpenSaveDialog('', '', DefOpenSaveDlgOptions );
        OSD.Filter := 'PNG - Portable Network Graphics|*.png';
        OSD.Filename := FileName;
        OSD.WndOwner := OpWnd;
        OSD.OpenDialog := false;
        if OSD.Execute then
          begin
          Image.SaveToFile(OSD.Filename);
          end;
        OSD.Free;
        end
      else Image.SaveToFile(FileName);
    finally
      fBitmap.Free;
      Image.Free;
      ReleaseDC(AHandle, DC);
    end;
end;

Can anyone please translate it? The part with OpenSaveDialog is not needed, i need only the screenshot action.

Could you just work out the coordinates of the client area and use _ScreenCapture_Capture?

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Could you just work out the coordinates of the client area and use _ScreenCapture_Capture?

It's not really client area, it's a control inside a window (it's a browser's window). But this control changes every session (ClassName Instance changed), and this example on Delphi somehow can get the handle of that control and take a screenshot from it.

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

It's not really client area, it's a control inside a window (it's a browser's window). But this control changes every session (ClassName Instance changed), and this example on Delphi somehow can get the handle of that control and take a screenshot from it.

The code doesn't look like it looks for any control. It's passed a handle of which it then gets the coords for with GetClientRect(). So you're missing the part of the code you really need.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

The code doesn't look like it looks for any control. It's passed a handle of which it then gets the coords for with GetClientRect(). So you're missing the part of the code you really need.

Well, actualy i have the all code, i just thought that this will be enough (it's a big program to save pages (in Opera) in quick mode). This function called like this:

If (Param1 = '/screenshot') Or (Param1 = '/png') Then
    begin
    EnumChildWindows(OpWnd, @ScreenEnum, Integer(@ScreenWnd));
    OpWinWnd := FindWindowEx(FindWindowEx(FindWindowEx(FindWindowEx(ScreenWnd, 0, 'OperaWindowClass', nil), 0, 'OpWindow', nil), 0, 'OperaWindowClass', nil), 0, 'OperaWindowClass', nil);
    If OpWinWnd = 0 Then OpWinWnd := OpWnd;
    If GetForegroundWindow <> OpWnd Then
      begin
        if IsIconic(OpWnd) then ShowWindow(OpWnd, SW_RESTORE);
        SetForegroundWindow(OpWnd);
        time := GetTickCount + TimeOut*2;
        While (GetForegroundWindow <> OpWnd) And (GetTickCount < time) Do Sleep(200);
        Sleep(500)
      end;
      If quick Then
        begin
        If all Then OpWinWnd := OpWnd;
        If QuickText <> '' Then NameFile := QuickText;
        rez := QuickSaveName(QuickSaveDir+NameFile+'.png');
        GetScreenShot(OpWinWnd, rez, True);
        If history Then AddToHistory(NameFile, rez, Param3, RootQsd, all);
        end
      Else
        begin
        If (ParamNum=2) And (Param2 <> '') Then NameFile := CorrectName(Param2);
        GetScreenShot(OpWinWnd, NameFile + '.png', false);
        end;
    end
Else
  ....
end

OpWnd is a main window handle. And the Params is the command line arguments.


 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

I think i got the "pattern" that used when classes is created. The main class (that i need the screenshot from) is always OperaWindowClass, and it's numerated always(?) as last, so the only thing i have to do now, is to get the numerated classses list (i wrote a function for this some time ago :P ), and get the last one (visible)... this example seems to work on different (Opera) versions and in different (process) sessions:

#Include <ScreenCapture.au3>

$sImageFile = @ScriptDir & "\OperaPage_ScreenShot.png"

_SaveOperaPageScreenShot($sImageFile)
ShellExecute($sImageFile)

Func _SaveOperaPageScreenShot($sFile)
    Local $hOpWnd = 0
    Local $aOpWinList = WinList("[CLASS:OpWindow]")
    
    For $i = 1 To UBound($aOpWinList)-1
        If BitAND(WinGetState($aOpWinList[$i][1]), 2) Then
            $hOpWnd = $aOpWinList[$i][1]
            ExitLoop
        EndIf
    Next
    
    If Not $hOpWnd Then Return SetError(1, 0, 0)
    
    Local $aClassList = StringSplit(_WinGetNumeratedClassList($hOpWnd), @CRLF, 1)
    Local $hCapt_Wnd, $iClassInstance, $iLastInstance = 0
    
    For $i = 1 To $aClassList[0]
        If Not StringRegExp($aClassList[$i], "OperaWindowClass") Then ContinueLoop
        
        $iClassInstance = Number(StringRegExpReplace($aClassList[$i], ".*(\d+)$", "\1"))
        
        If $iClassInstance > $iLastInstance Then
            $hCapt_Wnd = ControlGetHandle($hOpWnd, "", $aClassList[$i])
            $iLastInstance = $iClassInstance
        EndIf
    Next
    
    WinActivate($hOpWnd)
    WinWaitActive($hOpWnd)
    
    Return _ScreenCapture_CaptureWnd($sFile, $hCapt_Wnd, 0, 0, -1, -1, False)
EndFunc

Func _WinGetNumeratedClassList($sTitle, $sText="")
    Local $sClassList = WinGetClassList($sTitle, $sText)
    Local $aClassList = StringSplit($sClassList, @CRLF)
    Local $sRetClassList = "", $sHold_List = "|"
    Local $aiInHold, $iInHold
    
    For $i = 1 To UBound($aClassList) - 1
        If $aClassList[$i] = "" Then ContinueLoop
        
        If StringRegExp($sHold_List, "\|" & $aClassList[$i] & "~(\d+)\|") Then
            $aiInHold = StringRegExp($sHold_List, ".*\|" & $aClassList[$i] & "~(\d+)\|.*", 1)
            $iInHold = Number($aiInHold[UBound($aiInHold)-1])
            
            If $iInHold = 0 Then $iInHold += 1
            
            $aClassList[$i] &= "~" & $iInHold + 1
            $sHold_List &= $aClassList[$i] & "|"
            
            $sRetClassList &= $aClassList[$i] & @CRLF
        Else
            $aClassList[$i] &= "~1"
            $sHold_List &= $aClassList[$i] & "|"
            $sRetClassList &= $aClassList[$i] & @CRLF
        EndIf
    Next
    
    Return StringReplace(StringStripWS($sRetClassList, 3), "~", "")
EndFunc

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

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  
Followers 0