screen capture

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
npocmaka_
Posts: 516
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

screen capture

#1 Post by npocmaka_ » 14 Jun 2015 09:39

It relies on built in Win7/8 PSR tool and its command line interface.
For Vista/Xp this should be a replacement:

Code: Select all

https://technet.microsoft.com/en-us/magazine/2009.03.utilityspotlight2.aspx?pr=blog


though I don't know if it has the same command line interface.

Here's the PSR command line interface:

Code: Select all

https://social.technet.microsoft.com/Forums/office/en-US/b78253b1-6e38-4563-9efa-4973414e9a75/problems-step-recorder-psrexe-command-line-options?forum=w7itprogeneral



And despite it works directly in command prompt I couldn't make it work form batch file - either it hangs or when start command is used it creates non-stoppable instances.


The idea - it creates a zip file with .mht file inside where the screeshots are encoded in base64 - which can e decided with certutil

Code: Select all

@if (@X)==(@Y) @end /* JScript comment
   @echo off
   
      if "%~1" equ "" set "saveas=screenshot.jpg"  else  set "saveas=%~1"
      ::a skip that tests what happens with existing psr archive
      rem goto :skip
      :: remove and create the temp directory
      rd /S /Q "%temp%\--scrcapt--" 1>nul 2>&1
      md "%temp%\--scrcapt--" 1>nul 2>&1
      
      :: stop recorder just in case
      rem psr /stop 1>nul 2>&1
      rem taskkill /im psr.exe /f 1>nul 2>&1
      :: starts problem step recorder
      start "" psr /start  /output "%temp%\--scrcapt--\sc.zip" /sc 1 /maxsc 1 /sketch 0 /slides 0 /gui 0 /arcetl 0 /arcxml 0
      :: echo waits for 2 seconds
      w32tm /stripchart /computer:localhost /period:2 /dataonly /samples:2  >nul 2>&1
      :: stops the recorder
      start "" psr /stop
      :: waits for 2 seconds
      rem w32tm /stripchart /computer:localhost /period:1 /dataonly /samples:2  >nul 2>&1
      ::taskkill /im psr.exe /f 1>nul 2>&1
      :skip
      :: unzip the psr record
      cscript //E:JScript //nologo "%~f0" "%temp%\--scrcapt--\sc.zip" "%temp%\--scrcapt--"
      
      :: get the line where the base 64 string of the picture begins
      for /f "tokens=3 delims=:" %%# in ('
         findstr /n /b /c:"Content-Location: screenshot" "%temp%\--scrcapt--\Recording_*.mht"
      ') do (
         set /a "line=%%#+1"
      )
      echo %line%
      :: create b64 file certutil format
      echo -----BEGIN CERTIFICATE----->"%temp%\--scrcapt--\b64"
      more "%temp%\--scrcapt--\Recording_*.mht" +%line%|find /v "--=_NextPart_SMP">>"%temp%\--scrcapt--\b64"
      echo -----END CERTIFICATE----->>"%temp%\--scrcapt--\b64"
      
      :: decodes the file
      certutil -decode "%temp%\--scrcapt--\b64" "%saveas%" 1>nul 2>&1
   
   exit /b %errorlevel%
      
@if (@X)==(@Y) @end JScript comment */


var ShellObj=new ActiveXObject("Shell.Application");
var ARGS = WScript.Arguments;
var zipped=ShellObj.NameSpace(ARGS.Item(0)).Items();
var unzipTo=ShellObj.NameSpace(ARGS.Item(1));
unzipTo.MoveHere(zipped,4);
//have no idea if making the objects nulls will increase performance but in all vbs examples it is done in that way
zipped=null;
unzipTo=null;
ShellObj=null;



Its easy with C# hybrid but wanted something without redundant echoes and temporary compiled files.
Last edited by npocmaka_ on 22 Jul 2015 10:53, edited 1 time in total.

OperatorGK
Posts: 66
Joined: 13 Jan 2015 06:55

Re: My failed attempt to take a screenshot with a batch file

#2 Post by OperatorGK » 14 Jun 2015 11:32

Its easy with C# hybrid but wanted something without redundant echoes and temporary compiled files.

Use BAT/Powershell hybrid. Powershell has the same possibilities as C# but doesn't require compilation.

npocmaka_
Posts: 516
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: My failed attempt to take a screenshot with a batch file

#3 Post by npocmaka_ » 14 Jun 2015 14:30

OperatorGK wrote:
Its easy with C# hybrid but wanted something without redundant echoes and temporary compiled files.

Use BAT/Powershell hybrid. Powershell has the same possibilities as C# but doesn't require compilation.


powershell does not have PInvoke (shich is needed in this case) and generics though its possible to embed c# code into powershell


carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: My failed attempt to take a screenshot with a batch file

#5 Post by carlos » 15 Jun 2015 06:30

I'm sure that exists a code using mshta and excel object for take screenshots. But, i not found it.

npocmaka_
Posts: 516
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: My failed attempt to take a screenshot with a batch file

#6 Post by npocmaka_ » 15 Jun 2015 06:40

carlos wrote:I'm sure that exists a code using mshta and excel object for take screenshots. But, i not found it.


There's a clipboard data accessible through InternetExplorer.application :

Code: Select all

https://msdn.microsoft.com/en-us/library/ms535220(v=vs.85).aspx

(also mshta has it) and is also possible to press prtScr with send keys ("(%{1068})")

but I'm not sure if clipboardData object can be used for binary content.

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: My failed attempt to take a screenshot with a batch file

#7 Post by carlos » 15 Jun 2015 22:57

I found this in my archive:

Code: Select all

@mshta vbscript:execute("Set a=CreateObject(""Excel.Application""):a.ExecuteExcel4Macro(""CALL(""""user32"""",""""keybd_event"""",""""JJJJJ"""",44,0,1,0)""):a.ExecuteExcel4Macro(""CALL(""""user32"""",""""keybd_event"""",""""JJJJJ"""",44,0,2,0)""):close()")

npocmaka_
Posts: 516
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: My failed attempt to take a screenshot with a batch file

#8 Post by npocmaka_ » 16 Jun 2015 01:40

carlos wrote:I found this in my archive:

Code: Select all

@mshta vbscript:execute("Set a=CreateObject(""Excel.Application""):a.ExecuteExcel4Macro(""CALL(""""user32"""",""""keybd_event"""",""""JJJJJ"""",44,0,1,0)""):a.ExecuteExcel4Macro(""CALL(""""user32"""",""""keybd_event"""",""""JJJJJ"""",44,0,2,0)""):close()")



I have no office installed :?

taripo
Posts: 228
Joined: 01 Aug 2011 13:48

Re: My failed attempt to take a screenshot with a batch file

#9 Post by taripo » 17 Jun 2015 10:40

irfanview has a screenshot option - totally command line, no GUI popping up.

Code: Select all

C:\Program Files (x86)\IrfanView>i_view32.exe /capture=0 /convert=c:\blah\pic.jpg

npocmaka_
Posts: 516
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: My failed attempt to take a screenshot with a batch file

#10 Post by npocmaka_ » 22 Jul 2015 09:46

here's a .net selfcompiled script that takes a screen capture from the whole screen or from the active window:

Code: Select all

// 2>nul||@goto :batch
/*
:batch
@echo off
setlocal

:: find csc.exe
set "frm=%SystemRoot%\Microsoft.NET\Framework\"
for /f "tokens=* delims=" %%v in ('dir /b /a:d  /o:-n "%SystemRoot%\Microsoft.NET\Framework\v?.*"') do (
   set netver=%%v
   goto :break_loop
)
:break_loop
set csc=%frm%%netver%\csc.exe
if not exist "%~n0.exe" (
   %csc% /nologo /out:"%~n0.exe" "%~dpsfnx0" || (
      exit /b %errorlevel%
   )
)
%~n0.exe %*
endlocal & exit /b %errorlevel%

*/

// reference 
// https://gallery.technet.microsoft.com/scriptcenter/eeff544a-f690-4f6b-a586-11eea6fc5eb8

using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;

  /// Provides functions to capture the entire screen, or a particular window, and save it to a file.

  public class ScreenCapture
  {

    /// Creates an Image object containing a screen shot the active window

    public Image CaptureActiveWindow()
    {
      return CaptureWindow( User32.GetForegroundWindow() );
    }

    /// Creates an Image object containing a screen shot of the entire desktop

    public Image CaptureScreen()
    {
      return CaptureWindow( User32.GetDesktopWindow() );
    }     

    /// Creates an Image object containing a screen shot of a specific window

    private Image CaptureWindow(IntPtr handle)
    {
      // get te hDC of the target window
      IntPtr hdcSrc = User32.GetWindowDC(handle);
      // get the size
      User32.RECT windowRect = new User32.RECT();
      User32.GetWindowRect(handle,ref windowRect);
      int width = windowRect.right - windowRect.left;
      int height = windowRect.bottom - windowRect.top;
      // create a device context we can copy to
      IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
      // create a bitmap we can copy it to,
      // using GetDeviceCaps to get the width/height
      IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc,width,height);
      // select the bitmap object
      IntPtr hOld = GDI32.SelectObject(hdcDest,hBitmap);
      // bitblt over
      GDI32.BitBlt(hdcDest,0,0,width,height,hdcSrc,0,0,GDI32.SRCCOPY);
      // restore selection
      GDI32.SelectObject(hdcDest,hOld);
      // clean up
      GDI32.DeleteDC(hdcDest);
      User32.ReleaseDC(handle,hdcSrc);
      // get a .NET image object for it
      Image img = Image.FromHbitmap(hBitmap);
      // free up the Bitmap object
      GDI32.DeleteObject(hBitmap);
      return img;
    }

    public void CaptureActiveWindowToFile(string filename, ImageFormat format)
    {
      Image img = CaptureActiveWindow();
      img.Save(filename,format);
    }

    public void CaptureScreenToFile(string filename, ImageFormat format)
    {
      Image img = CaptureScreen();
      img.Save(filename,format);
    }
   
   static bool fullscreen=true;
   static String file="screenshot.bmp";
   static System.Drawing.Imaging.ImageFormat format=System.Drawing.Imaging.ImageFormat.Bmp;
   
   static void parseArguments()
   {
      String[] arguments = Environment.GetCommandLineArgs();
      if (arguments.Length == 1)
      {
         printHelp();
         Environment.Exit(0);
      }
      if (arguments[1].ToLower().Equals("/h") || arguments[1].ToLower().Equals("/help"))
      {
         printHelp();
         Environment.Exit(0);
      }
      
      file=arguments[1];
      
      if (arguments.Length > 2)
      {
         switch(arguments[2].ToLower())
         {
            case "bmp":
               format=System.Drawing.Imaging.ImageFormat.Bmp;
               break;
            case "emf":
               format=System.Drawing.Imaging.ImageFormat.Emf;
               break;
            case "exif":
               format=System.Drawing.Imaging.ImageFormat.Exif;
               break;
            case "gif":
               format=System.Drawing.Imaging.ImageFormat.Gif;
               break;
            case "icon":
               format=System.Drawing.Imaging.ImageFormat.Icon;
               break;
            case "jpeg":
            case "jpg":
               format=System.Drawing.Imaging.ImageFormat.Jpeg;
               break;
            case "png":
               format=System.Drawing.Imaging.ImageFormat.Png;
               break;
            case "tiff":
               format=System.Drawing.Imaging.ImageFormat.Tiff;
               break;
            default:
               Console.WriteLine("invalid image format " + arguments[2]);
               Environment.Exit(5);
               break;
               
         }
      }
      
      if (arguments.Length > 3)
      {
         switch(arguments[3].ToLower()){
            case "y":
               fullscreen=true;
               break;
            case "n":
               fullscreen=false;
               break;
            default:
               Console.WriteLine("invalid image mode " + arguments[3]);
               Environment.Exit(6);
               break;
         }
      }
      
   }
   
   static void  printHelp()
   {
      String scriptName=Environment.GetCommandLineArgs()[0];
      scriptName=scriptName.Substring (0,scriptName.Length);
      Console.WriteLine(scriptName + " captures the screen or the active window and saves it to a file");
      Console.WriteLine(scriptName + " filename [format] [Y|N]");
      Console.WriteLine("finename - the file where the screen capture will be saved");
      Console.WriteLine("format - Bmp,Emf,Exif,Gif,Icon,Jpeg,Png,Tiff and are supported - default is bmp");
      Console.WriteLine("Y|N - either or not the whole screen to be captured (if no only active window will be processed).Defalut is Y");
   }
   
   public static void Main()
   {
      parseArguments();
      ScreenCapture sc=new ScreenCapture();
      try
      {
         if (fullscreen){
            Console.WriteLine("Taking a capture of the whole screen to " + file + " as " + format);
            sc.CaptureScreenToFile(file,format);
         } else {
            Console.WriteLine("Taking a capture of the active window to " + file + " as " + format);
            sc.CaptureActiveWindowToFile(file,format);
         }
      } catch (Exception e) {
         Console.WriteLine("Check if file path is valid");
         Console.WriteLine(e.ToString());
      }
   }

    /// Helper class containing Gdi32 API functions
 
    private class GDI32
    {
       
      public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter
      [DllImport("gdi32.dll")]
      public static extern bool BitBlt(IntPtr hObject,int nXDest,int nYDest,
        int nWidth,int nHeight,IntPtr hObjectSource,
        int nXSrc,int nYSrc,int dwRop);
      [DllImport("gdi32.dll")]
      public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC,int nWidth,
        int nHeight);
      [DllImport("gdi32.dll")]
      public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
      [DllImport("gdi32.dll")]
      public static extern bool DeleteDC(IntPtr hDC);
      [DllImport("gdi32.dll")]
      public static extern bool DeleteObject(IntPtr hObject);
      [DllImport("gdi32.dll")]
      public static extern IntPtr SelectObject(IntPtr hDC,IntPtr hObject);
    }
 

    /// Helper class containing User32 API functions

    private class User32
    {
      [StructLayout(LayoutKind.Sequential)]
      public struct RECT
      {
        public int left;
        public int top;
        public int right;
        public int bottom;
      }
      [DllImport("user32.dll")]
      public static extern IntPtr GetDesktopWindow();
      [DllImport("user32.dll")]
      public static extern IntPtr GetWindowDC(IntPtr hWnd);
      [DllImport("user32.dll")]
      public static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hDC);
      [DllImport("user32.dll")]
      public static extern IntPtr GetWindowRect(IntPtr hWnd,ref RECT rect);
      [DllImport("user32.dll")]
      public static extern IntPtr GetForegroundWindow();       
    }
  }

 


I've used this as a reference - https://gallery.technet.microsoft.com/s ... eea6fc5eb8

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: screen capture

#11 Post by carlos » 22 Jul 2015 18:35

I tested ok with a png image.

consider replace:

Code: Select all

// 2>nul||@goto :batch

by:

Code: Select all

@set @Batch=1 /*

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: screen capture

#12 Post by foxidrive » 22 Jul 2015 21:45

I had some trouble with the wrong screen dimensions in Windows 8.1 32 bit

Here is the actual window image and the smaller dimensions of your utility.

Image

Image

It showed the same problem in JPG and in full screen.


It occurs to me that a switch for window title would be useful to capture a target window,
and taking the filetype from the extension of the filename would save typing.

npocmaka_
Posts: 516
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: screen capture

#13 Post by npocmaka_ » 23 Jul 2015 02:03

carlos wrote:I tested ok with a png image.

consider replace:

Code: Select all

// 2>nul||@goto :batch

by:

Code: Select all

@set @Batch=1 /*


Unfortunately this is a C# and I cant use jscript.net directives .And jscript.net does not support DllImport and pinvoke.
I've spent some time wondering how to suppress the nasty output with c# hybrids but this is the best approach I've find out - http://stackoverflow.com/questions/3094 ... ine-with-c - i.e. modifying console buffer.Which will not work so great if the output is redirected to a file or piped to another command.

May'be XAML/WPF Application could be helpful - I think it allows to mix c# and xml - but didn't go so far.


Calling the file without extension eventually helps as it trigger first the '.exe' if it's exists.

foxidrive wrote:It occurs to me that a switch for window title would be useful to capture a target window,
and taking the filetype from the extension of the filename would save typing.



hmmmmmmm...... yes :!: I will try to update it today.

Thank both of you for the feedback.

npocmaka_
Posts: 516
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: screen capture

#14 Post by npocmaka_ » 23 Jul 2015 06:06

here's a new version (and here)
now it can accept two arguments - one for the file and the second for the window title/active window (more in the help message).Though its not working good it the window is minimized.


As for the cropped screenshots - I have no idea what I can do at the moment. Will test on different machines to see whats going on.

For AppActivate I'm using Microsoft.VisualBasic namespace which should be referred during compilation:


Code: Select all

// 2>nul||@goto :batch
/*
:batch
@echo off
setlocal

:: find csc.exe
set "csc="
for /r "%SystemRoot%\Microsoft.NET\Framework\" %%# in ("*csc.exe") do  set "csc=%%#"

if not exist "%csc%" (
   echo no .net framework installed
   exit /b 10
)

if not exist "%~n0.exe" (
   %csc% /nologo /r:"Microsoft.VisualBasic.dll" /out:"%~n0.exe" "%~dpsfnx0" || (
      exit /b %errorlevel%
   )
)
%~n0.exe %*
endlocal & exit /b %errorlevel%

*/

// reference 
// https://gallery.technet.microsoft.com/scriptcenter/eeff544a-f690-4f6b-a586-11eea6fc5eb8

using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections.Generic;
using Microsoft.VisualBasic;


/// Provides functions to capture the entire screen, or a particular window, and save it to a file.

public class ScreenCapture
{

    /// Creates an Image object containing a screen shot the active window

    public Image CaptureActiveWindow()
    {
        return CaptureWindow(User32.GetForegroundWindow());
    }

    /// Creates an Image object containing a screen shot of the entire desktop

    public Image CaptureScreen()
    {
        return CaptureWindow(User32.GetDesktopWindow());
    }

    /// Creates an Image object containing a screen shot of a specific window

    private Image CaptureWindow(IntPtr handle)
    {
        // get te hDC of the target window
        IntPtr hdcSrc = User32.GetWindowDC(handle);
        // get the size
        User32.RECT windowRect = new User32.RECT();
        User32.GetWindowRect(handle, ref windowRect);
        int width = windowRect.right - windowRect.left;
        int height = windowRect.bottom - windowRect.top;
        // create a device context we can copy to
        IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
        // create a bitmap we can copy it to,
        // using GetDeviceCaps to get the width/height
        IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
        // select the bitmap object
        IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);
        // bitblt over
        GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);
        // restore selection
        GDI32.SelectObject(hdcDest, hOld);
        // clean up
        GDI32.DeleteDC(hdcDest);
        User32.ReleaseDC(handle, hdcSrc);
        // get a .NET image object for it
        Image img = Image.FromHbitmap(hBitmap);
        // free up the Bitmap object
        GDI32.DeleteObject(hBitmap);
        return img;
    }

    public void CaptureActiveWindowToFile(string filename, ImageFormat format)
    {
        Image img = CaptureActiveWindow();
        img.Save(filename, format);
    }

    public void CaptureScreenToFile(string filename, ImageFormat format)
    {
        Image img = CaptureScreen();
        img.Save(filename, format);
    }

    static bool fullscreen = true;
    static String file = "screenshot.bmp";
    static System.Drawing.Imaging.ImageFormat format = System.Drawing.Imaging.ImageFormat.Bmp;
    static String windowTitle = "";

    static void parseArguments()
    {
        String[] arguments = Environment.GetCommandLineArgs();
        if (arguments.Length == 1)
        {
            printHelp();
            Environment.Exit(0);
        }
        if (arguments[1].ToLower().Equals("/h") || arguments[1].ToLower().Equals("/help"))
        {
            printHelp();
            Environment.Exit(0);
        }

        file = arguments[1];
        Dictionary<String, System.Drawing.Imaging.ImageFormat> formats =
        new Dictionary<String, System.Drawing.Imaging.ImageFormat>();

        formats.Add("bmp", System.Drawing.Imaging.ImageFormat.Bmp);
        formats.Add("emf", System.Drawing.Imaging.ImageFormat.Emf);
        formats.Add("exif", System.Drawing.Imaging.ImageFormat.Exif);
        formats.Add("jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
        formats.Add("jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
        formats.Add("gif", System.Drawing.Imaging.ImageFormat.Gif);
        formats.Add("png", System.Drawing.Imaging.ImageFormat.Png);
        formats.Add("tiff", System.Drawing.Imaging.ImageFormat.Tiff);
        formats.Add("wmf", System.Drawing.Imaging.ImageFormat.Wmf);


        String ext = "";
        if (file.LastIndexOf('.') > -1)
        {
            ext = file.ToLower().Substring(file.LastIndexOf('.') + 1, file.Length - file.LastIndexOf('.') - 1);
        }
        else
        {
            Console.WriteLine("Invalid file name - no extension");
            Environment.Exit(7);
        }

        try
        {
            format = formats[ext];
        }
        catch (Exception e)
        {
            Console.WriteLine("Probably wrong file format:" + ext);
            Console.WriteLine(e.ToString());
            Environment.Exit(8);
        }


        if (arguments.Length > 2)
        {
            windowTitle = arguments[2];
            fullscreen = false;
        }

    }

    static void printHelp()
    {
        //clears the extension from the script name
        String scriptName = Environment.GetCommandLineArgs()[0];
        scriptName = scriptName.Substring(0, scriptName.Length);
        Console.WriteLine(scriptName + " captures the screen or the active window and saves it to a file.");
        Console.WriteLine("");
        Console.WriteLine("Usage:");
      Console.WriteLine("");
        Console.WriteLine(" " + scriptName + " filename  [WindowTitle]");
        Console.WriteLine("finename - the file where the screen capture will be saved");
        Console.WriteLine("     allowed file extensions are - Bmp,Emf,Exif,Gif,Icon,Jpeg,Png,Tiff,Wmf.");
        Console.WriteLine("WindowTitle - instead of capture whole screen you can point to a window ");
        Console.WriteLine("     with a title which will put on focus and captuted.");
        Console.WriteLine("     For WindowTitle you can pass only the first few characters.");
        Console.WriteLine("     If don't want to change the current active window pass only \"\"");
    }

    public static void Main()
    {
        parseArguments();
        ScreenCapture sc = new ScreenCapture();
        if (!fullscreen && !windowTitle.Equals(""))
        {
            try
            {

                Interaction.AppActivate(windowTitle);
                Console.WriteLine("setting " + windowTitle + " on focus");
            }
            catch (Exception e)
            {
                Console.WriteLine("Probably there's no window like " + windowTitle);
                Console.WriteLine(e.ToString());
                Environment.Exit(9);
            }


        }
        try
        {
            if (fullscreen)
            {
                Console.WriteLine("Taking a capture of the whole screen to " + file);
                sc.CaptureScreenToFile(file, format);
            }
            else
            {
                Console.WriteLine("Taking a capture of the active window to " + file);
                sc.CaptureActiveWindowToFile(file, format);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Check if file path is valid " + file);
            Console.WriteLine(e.ToString());
        }
    }

    /// Helper class containing Gdi32 API functions

    private class GDI32
    {

        public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter
        [DllImport("gdi32.dll")]
        public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest,
          int nWidth, int nHeight, IntPtr hObjectSource,
          int nXSrc, int nYSrc, int dwRop);
        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth,
          int nHeight);
        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
        [DllImport("gdi32.dll")]
        public static extern bool DeleteDC(IntPtr hDC);
        [DllImport("gdi32.dll")]
        public static extern bool DeleteObject(IntPtr hObject);
        [DllImport("gdi32.dll")]
        public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
    }


    /// Helper class containing User32 API functions

    private class User32
    {
        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }
        [DllImport("user32.dll")]
        public static extern IntPtr GetDesktopWindow();
        [DllImport("user32.dll")]
        public static extern IntPtr GetWindowDC(IntPtr hWnd);
        [DllImport("user32.dll")]
        public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
        [DllImport("user32.dll")]
        public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
        [DllImport("user32.dll")]
        public static extern IntPtr GetForegroundWindow();
    }
}

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: screen capture

#15 Post by foxidrive » 25 Jul 2015 07:06

Just confirming that it works here too, though the truncated screens still appear.

Post Reply