Jump to content

Recommended Posts

Posted (edited)

Merry Christmas!

Dear friends, during Christmas we share gifts, so I did not come empty handed, but I have gifts for you.

C# scripts that you can compile yourself on your Windows computers (sorry Linux geeks!).

 
ScreenCapture
 
Compilation:

On Windows 7+ it should work without any additional installation.

On Vista change the version from v3.5 to v3.0, or install .NET Framework v3.5, if needed.

On WinXP install .NET Framework v3.5+, if needed.

Start, cmd,

cd [the location where you stored ScreenCapture.cs]
%SYSTEMROOT%\Microsoft.NET\Framework\v3.5\csc ScreenCapture.cs

If you are having problems, you can change from v3.5 to the version you have installed. Verify it by

dir %SYSTEMROOT%\Microsoft.NET\Framework\v*

 

 

Usage:

e.g.

ScreenCapture -d -v %TEMP%\picture.png

ScreenCapture -d D:\screen-shots\picture.jpg

ScreenCapture D:\screen-shots\image.bmp

 

It will generate image files e.g.

picture_20211226_173530.png

picture_20211226_173532.jpg

image.bmp

with datestamp i timestamp included in the file name (thanks to -d option). File name is unique per second.

If you want to add milliseconds, modify the dateformat string in line #115 to e.g.

         string dateformat = "_yyyyMMdd_HHmmss_fff";

https://docs.microsoft.com/en-us/dotnet/standard/base-types/how-to-display-milliseconds-in-date-and-time-values

 

Assign it to the 3rd mouse button and/or hotkey for quick access and press on demand.

Image files will be created in the desired location.

 

Source code:

// ScreenCapture v1.0.1 (c) 2021 Sensei
// Capture the screen to an image file
//
// Usage:
// ScreenCapture [-d|--datestamp] [-v|--verbose] filename
//
// Compilation:
// %SYSTEMROOT%\Microsoft.NET\Framework\v3.5\csc ScreenCapture.cs
//

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections.Generic;

public class ScreenCapture {
   private class GDI32 {
      public const int SRCCOPY = 0x00CC0020;
      [DllImport("gdi32.dll")]
      public static extern bool BitBlt( IntPtr hDCDst, int nXDst, int nYDst, int nWidth, int nHeight, IntPtr hDCSrc, 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 );
   }

   private class User32 {
      [StructLayout(LayoutKind.Sequential)]
      public struct Rect {
         public int left, top, right, 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 GetWindowRect( IntPtr hWnd, ref Rect rect );
      [DllImport("user32.dll")]
      public static extern IntPtr ReleaseDC( IntPtr hWnd, IntPtr hDC );
      [DllImport("user32.dll")]
      public static extern int SetProcessDPIAware();
   }

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

   public static Image CaptureWindow( IntPtr hWnd ) {
      IntPtr hDCSrc = User32.GetWindowDC( hWnd );
      User32.Rect rect = new User32.Rect();
      User32.GetWindowRect( hWnd, ref rect );
      int width = rect.right - rect.left;
      int height = rect.bottom - rect.top;
      IntPtr hDCDst = GDI32.CreateCompatibleDC( hDCSrc );
      IntPtr hBitmap = GDI32.CreateCompatibleBitmap( hDCSrc, width, height );
      IntPtr hBitmapOld = GDI32.SelectObject( hDCDst, hBitmap );
      GDI32.BitBlt( hDCDst, 0, 0, width, height, hDCSrc, 0, 0, GDI32.SRCCOPY );
      GDI32.SelectObject( hDCDst, hBitmapOld );
      GDI32.DeleteDC( hDCDst );
      User32.ReleaseDC( hWnd, hDCSrc );
      Image image = Image.FromHbitmap( hBitmap );
      GDI32.DeleteObject( hBitmap );
      return( image );
   }

   public static void CaptureScreenToFile( string filename ) {
      string extension = Path.GetExtension( filename );
      if( String.IsNullOrEmpty( extension ) ) throw( new Exception( "Missing extension" ) );
      ImageFormat format = GetImageFormatByExtension( extension );
      Image image = CaptureScreen();
      image.Save( filename, format );
   }

   public static ImageFormat GetImageFormatByExtension( string extension ) {
      Dictionary<String, ImageFormat> formats = new Dictionary<String, ImageFormat>();
      formats.Add( "bmp", ImageFormat.Bmp );
      formats.Add( "emf", ImageFormat.Emf );
      formats.Add( "exif", ImageFormat.Exif );
      formats.Add( "jpg", ImageFormat.Jpeg );
      formats.Add( "jpeg", ImageFormat.Jpeg );
      formats.Add( "gif", ImageFormat.Gif );
      formats.Add( "png", ImageFormat.Png );
      formats.Add( "tiff", ImageFormat.Tiff );
      formats.Add( "wmf", ImageFormat.Wmf );

      try {
         extension = extension.Substring( 1 );
         extension = extension.ToLower();
         return( formats[ extension ] );
      } catch( Exception e ) {
         throw( new Exception( "Unknown extension " + extension, e ) );
      }
   }

   public static void Help() {
      Console.WriteLine( "ScreenCapture v1.0.1 (c) 2021 Sensei" );
      Console.WriteLine( "Usage:" );
      Console.WriteLine( "ScreenCapture [-d|--datestamp] [-v|--verbose] filename" );
   }

   public static void Main( string [] args ) {
      if( Environment.OSVersion.Version.Major >= 6 ) {
         User32.SetProcessDPIAware(); // Added in Vista OS.
      }

      if( args.Length > 0 ) {
        string dateformat = "_yyyyMMdd_HHmmss";
        bool datestamp = false;
        bool verbose = false;
        for( int i = args.Length - 2; i >= 0; i-- ) {
           string arg = args[i];
           if( arg.Equals( "-d" ) || arg.Equals( "--datestamp" ) ) {
              datestamp = true;
           } else if( arg.Equals( "-v" ) || arg.Equals( "--verbose" ) ) {
              verbose = true;
           }
        }

        string filename = args[ args.Length - 1 ];
        if( datestamp ) {
           string extension = Path.GetExtension( filename );
           filename = filename.Substring( 0, filename.Length - extension.Length );
           filename = filename + DateTime.Now.ToString( dateformat ) + extension;
        }

        try {
           CaptureScreenToFile( filename );
           if( verbose ) Console.WriteLine( "Screen captured to " + filename );
        } catch( Exception e ) {
           Console.WriteLine( e.Message );
        }
     } else {
        Help();
     }
  }
}

 

Please don't hesitate to share your ideas for improvement.

 

ScreenCapture.cs

Edited by Sensei

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
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.