Capture the current screen and save it as a picture

From , 5 Years ago, written in C#, viewed 170 times.
URL https://pastebin.vip/view/53420bd6
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Diagnostics;
  5. using System.Drawing;
  6. using System.Drawing.Imaging;
  7. using System.IO;
  8. using System.Runtime.InteropServices;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Windows.Forms;
  12.  
  13. namespace RobvanderWoude
  14. {
  15.         class PrintScreen
  16.         {
  17.                 static int Main( string[] args )
  18.                 {
  19.                         try
  20.                         {
  21.                                 string output = string.Empty;
  22.                                 bool overwrite = false;
  23.                                 bool text = false;
  24.                                 ImageFormat type = null;
  25.  
  26.                                 #region Command Line parsing
  27.  
  28.                                 if ( args.Length == 0 )
  29.                                 {
  30.                                         return WriteError( );
  31.                                 }
  32.  
  33.                                 foreach ( string arg in args )
  34.                                 {
  35.                                         switch ( arg.ToUpper( ).Substring( 0, 2 ) )
  36.                                         {
  37.                                                 case "/?":
  38.                                                         return WriteError( );
  39.                                                 case "/O":
  40.                                                         overwrite = true;
  41.                                                         break;
  42.                                                 case "/T":
  43.                                                         if ( text )
  44.                                                         {
  45.                                                                 return WriteError( "Cannot capture current window as bitmap" );
  46.                                                         }
  47.                                                         switch ( arg.ToUpper( ).Substring( 3 ) )
  48.                                                         {
  49.                                                                 case "BMP":
  50.                                                                         type = ImageFormat.Bmp;
  51.                                                                         break;
  52.                                                                 case "GIF":
  53.                                                                         type = ImageFormat.Gif;
  54.                                                                         break;
  55.                                                                 case "JPG":
  56.                                                                 case "JPEG":
  57.                                                                         type = ImageFormat.Jpeg;
  58.                                                                         break;
  59.                                                                 case "PNG":
  60.                                                                         type = ImageFormat.Png;
  61.                                                                         break;
  62.                                                                 case "TIF":
  63.                                                                 case "TIFF":
  64.                                                                         type = ImageFormat.Tiff;
  65.                                                                         break;
  66.                                                                 case "TXT":
  67.                                                                         text = true;
  68.                                                                         break;
  69.                                                                 default:
  70.                                                                         return WriteError( "Invalid file format: \"" + arg.Substring( 4 ) + "\"" );
  71.                                                         }
  72.                                                         break;
  73.                                                 default:
  74.                                                         output = arg;
  75.                                                         break;
  76.                                         }
  77.                                 }
  78.  
  79.                                 // Check if directory exists
  80.                                 if ( !Directory.Exists( Path.GetDirectoryName( output ) ) )
  81.                                 {
  82.                                         return WriteError( "Invalid path for output file: \"" + output + "\"" );
  83.                                 }
  84.  
  85.                                 // Check if file exists, and if so, if it can be overwritten
  86.                                 if ( File.Exists( output ) )
  87.                                 {
  88.                                         if ( overwrite )
  89.                                         {
  90.                                                 File.Delete( output );
  91.                                         }
  92.                                         else
  93.                                         {
  94.                                                 return WriteError( "File exists; use /O to overwrite existing files." );
  95.                                         }
  96.                                 }
  97.  
  98.                                 if ( type == null && text == false )
  99.                                 {
  100.                                         string ext = Path.GetExtension( output ).ToUpper( );
  101.                                         switch ( ext )
  102.                                         {
  103.                                                 case ".BMP":
  104.                                                         type = ImageFormat.Bmp;
  105.                                                         break;
  106.                                                 case ".GIF":
  107.                                                         type = ImageFormat.Gif;
  108.                                                         break;
  109.                                                 case ".JPG":
  110.                                                 case ".JPEG":
  111.                                                         type = ImageFormat.Jpeg;
  112.                                                         break;
  113.                                                 case ".PNG":
  114.                                                         type = ImageFormat.Png;
  115.                                                         break;
  116.                                                 case ".TIF":
  117.                                                 case ".TIFF":
  118.                                                         type = ImageFormat.Tiff;
  119.                                                         break;
  120.                                                 case ".TXT":
  121.                                                         text = true;
  122.                                                         break;
  123.                                                 default:
  124.                                                         return WriteError( "Invalid file type: \"" + ext + "\"" );
  125.                                                         return 1;
  126.                                         }
  127.                                 }
  128.  
  129.                                 #endregion Command Line parsing
  130.  
  131.                                 if ( text )
  132.                                 {
  133.                                         // Based on code by Simon Mourier (http://www.softfluent.com/)
  134.                                         // http://stackoverflow.com/questions/12355378/read-from-location-on-console-c-sharp
  135.                                         string readtext = string.Empty;
  136.                                         for ( short i = 0; i < (short) Console.BufferHeight; i++ )
  137.                                         {
  138.                                                 foreach ( string line in ConsoleReader.ReadFromBuffer( 0, i, (short) Console.BufferWidth, 1 ) )
  139.                                                 {
  140.                                                         readtext += line + "\n";
  141.                                                 }
  142.                                         }
  143.                                         StreamWriter file = new StreamWriter( output );
  144.                                         file.Write( readtext );
  145.                                         file.Close( );
  146.                                 }
  147.                                 else
  148.                                 {
  149.                                         // Based on code by Ali Hamdar (http://alihamdar.com/)
  150.                                         // http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/79efecc4-fa6d-4078-afe4-bb1379bb968b
  151.                                         // Default values for full screen
  152.                                         int width = Screen.PrimaryScreen.Bounds.Width;
  153.                                         int height = Screen.PrimaryScreen.Bounds.Height;
  154.                                         int top = 0;
  155.                                         int left = 0;
  156.                                         Bitmap printscreen = new Bitmap( width, height );
  157.                                         Graphics graphics = Graphics.FromImage( printscreen as Image );
  158.                                         graphics.CopyFromScreen( top, left, 0, 0, printscreen.Size );
  159.                                         printscreen.Save( output, type );
  160.                                 }
  161.                                 return 0;
  162.                         }
  163.                         catch ( Exception e )
  164.                         {
  165.                                 Console.Error.WriteLine( e.Message );
  166.                                 return 1;
  167.                         }
  168.                 }
  169.                 #region Error Handling
  170.  
  171.                 public static int WriteError( string errorMessage = "" )
  172.                 {
  173.                         /*
  174.                         PrintScreen,  Version 1.00
  175.                         Save a screenshot as image or save the current console buffer as text
  176.  
  177.                         Usage:   PRINTSCREEN  outputfile  [ /T:type ]  [ /O ]
  178.  
  179.                         Where:   outputfile   is the file to save the screenshot or text to
  180.                                  /T:type      specifies the file type: BMP, GIF, JPG, PNG, TIF or TXT
  181.                                               (only required if outputfile extension is different)
  182.                                  /O           overwrites an existing file
  183.                        
  184.                         Credits: Code to read console buffer by Simon Mourier http://www.softfluent.com
  185.                                  Code for graphic screenshot by Ali Hamdar    http://alihamdar.com
  186.                        
  187.                         Written by Rob van der Woude
  188.                         http://www.robvanderwoude.com
  189.                         */
  190.  
  191.                         Console.ResetColor( );
  192.                         if ( string.IsNullOrEmpty( errorMessage ) == false )
  193.                         {
  194.                                 Console.Error.WriteLine( );
  195.                                 Console.ForegroundColor = ConsoleColor.Red;
  196.                                 Console.Error.Write( "ERROR:  " );
  197.                                 Console.ForegroundColor = ConsoleColor.White;
  198.                                 Console.Error.WriteLine( errorMessage );
  199.                                 Console.ResetColor( );
  200.                         }
  201.                         Console.Error.WriteLine( );
  202.                         Console.Error.WriteLine( "PrintScreen,  Version 1.10" );
  203.                         Console.Error.WriteLine( "Save a screenshot as image or save the current console buffer as text" );
  204.                         Console.Error.WriteLine( );
  205.                         Console.Error.Write( "Usage:   " );
  206.                         Console.ForegroundColor = ConsoleColor.White;
  207.                         Console.Error.WriteLine( "PRINTSCREEN  outputfile  [ /T:type ]  [ /O ]" );
  208.                         Console.ResetColor( );
  209.                         Console.Error.WriteLine( );
  210.                         Console.Error.Write( "Where:   " );
  211.                         Console.ForegroundColor = ConsoleColor.White;
  212.                         Console.Error.Write( "outputfile" );
  213.                         Console.ResetColor( );
  214.                         Console.Error.WriteLine( "   is the file to save the screenshot or text to" );
  215.                         Console.ForegroundColor = ConsoleColor.White;
  216.                         Console.Error.Write( "         /T:type" );
  217.                         Console.ResetColor( );
  218.                         Console.Error.Write( "      specifies the file type: " );
  219.                         Console.ForegroundColor = ConsoleColor.White;
  220.                         Console.Error.Write( "BMP" );
  221.                         Console.ResetColor( );
  222.                         Console.Error.Write( ", " );
  223.                         Console.ForegroundColor = ConsoleColor.White;
  224.                         Console.Error.Write( "GIF" );
  225.                         Console.ResetColor( );
  226.                         Console.Error.Write( ", " );
  227.                         Console.ForegroundColor = ConsoleColor.White;
  228.                         Console.Error.Write( "JPG" );
  229.                         Console.ResetColor( );
  230.                         Console.Error.Write( ", " );
  231.                         Console.ForegroundColor = ConsoleColor.White;
  232.                         Console.Error.Write( "PNG" );
  233.                         Console.ResetColor( );
  234.                         Console.Error.Write( ", " );
  235.                         Console.ForegroundColor = ConsoleColor.White;
  236.                         Console.Error.Write( "TIF" );
  237.                         Console.ResetColor( );
  238.                         Console.Error.Write( " or " );
  239.                         Console.ForegroundColor = ConsoleColor.White;
  240.                         Console.Error.WriteLine( "TXT" );
  241.                         Console.ResetColor( );
  242.                         Console.Error.Write( "                      (only required if " );
  243.                         Console.ForegroundColor = ConsoleColor.White;
  244.                         Console.Error.Write( "outputfile" );
  245.                         Console.ResetColor( );
  246.                         Console.Error.WriteLine( " extension is different)" );
  247.                         Console.ForegroundColor = ConsoleColor.White;
  248.                         Console.Error.Write( "         /O" );
  249.                         Console.ResetColor( );
  250.                         Console.Error.WriteLine( "           overwrites an existing file" );
  251.                         Console.Error.WriteLine( );
  252.                         Console.Error.Write( "Credits: Code to read console buffer by Simon Mourier " );
  253.                         Console.ForegroundColor = ConsoleColor.DarkGray;
  254.                         Console.Error.WriteLine( "http://www.softfluent.com" );
  255.                         Console.ResetColor( );
  256.                         Console.Error.Write( "         Code for graphic screenshot by Ali Hamdar    " );
  257.                         Console.ForegroundColor = ConsoleColor.DarkGray;
  258.                         Console.Error.WriteLine( "http://alihamdar.com" );
  259.                         Console.ResetColor( );
  260.                         Console.Error.WriteLine( );
  261.                         Console.Error.WriteLine( "Written by Rob van der Woude" );
  262.                         Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  263.                         return 1;
  264.                 }
  265.                 #endregion Error Handling
  266.  
  267.         }
  268.  
  269.         #region Read From Console Buffer
  270.  
  271.         // ConsoleReader by Simon Mourier (http://www.softfluent.com/)
  272.         // http://stackoverflow.com/questions/12355378/read-from-location-on-console-c-sharp
  273.         public class ConsoleReader
  274.         {
  275.                 public static IEnumerable<string> ReadFromBuffer( short x, short y, short width, short height )
  276.                 {
  277.                         IntPtr buffer = Marshal.AllocHGlobal( width * height * Marshal.SizeOf( typeof( CHAR_INFO ) ) );
  278.                         if ( buffer == null )
  279.                                 throw new OutOfMemoryException( );
  280.  
  281.                         try
  282.                         {
  283.                                 COORD coord = new COORD( );
  284.                                 SMALL_RECT rc = new SMALL_RECT( );
  285.                                 rc.Left = x;
  286.                                 rc.Top = y;
  287.                                 rc.Right = (short) ( x + width - 1 );
  288.                                 rc.Bottom = (short) ( y + height - 1 );
  289.  
  290.                                 COORD size = new COORD( );
  291.                                 size.X = width;
  292.                                 size.Y = height;
  293.  
  294.                                 const int STD_OUTPUT_HANDLE = -11;
  295.                                 if ( !ReadConsoleOutput( GetStdHandle( STD_OUTPUT_HANDLE ), buffer, size, coord, ref rc ) )
  296.                                 {
  297.                                         // 'Not enough storage is available to process this command' may be raised for buffer size > 64K (see ReadConsoleOutput doc.)
  298.                                         throw new Win32Exception( Marshal.GetLastWin32Error( ) );
  299.                                 }
  300.  
  301.                                 IntPtr ptr = buffer;
  302.                                 for ( int h = 0; h < height; h++ )
  303.                                 {
  304.                                         StringBuilder sb = new StringBuilder( );
  305.                                         for ( int w = 0; w < width; w++ )
  306.                                         {
  307.                                                 CHAR_INFO ci = (CHAR_INFO) Marshal.PtrToStructure( ptr, typeof( CHAR_INFO ) );
  308.                                                 char[] chars = Console.OutputEncoding.GetChars( ci.charData );
  309.                                                 sb.Append( chars[0] );
  310.                                                 ptr += Marshal.SizeOf( typeof( CHAR_INFO ) );
  311.                                         }
  312.                                         yield return sb.ToString( );
  313.                                 }
  314.                         }
  315.                         finally
  316.                         {
  317.                                 Marshal.FreeHGlobal( buffer );
  318.                         }
  319.                 }
  320.  
  321.                 [StructLayout( LayoutKind.Sequential )]
  322.                 private struct CHAR_INFO
  323.                 {
  324.                         [MarshalAs( UnmanagedType.ByValArray, SizeConst = 2 )]
  325.                         public byte[] charData;
  326.                         public short attributes;
  327.                 }
  328.  
  329.                 [StructLayout( LayoutKind.Sequential )]
  330.                 private struct COORD
  331.                 {
  332.                         public short X;
  333.                         public short Y;
  334.                 }
  335.  
  336.                 [StructLayout( LayoutKind.Sequential )]
  337.                 private struct SMALL_RECT
  338.                 {
  339.                         public short Left;
  340.                         public short Top;
  341.                         public short Right;
  342.                         public short Bottom;
  343.                 }
  344.  
  345.                 [StructLayout( LayoutKind.Sequential )]
  346.                 private struct CONSOLE_SCREEN_BUFFER_INFO
  347.                 {
  348.                         public COORD dwSize;
  349.                         public COORD dwCursorPosition;
  350.                         public short wAttributes;
  351.                         public SMALL_RECT srWindow;
  352.                         public COORD dwMaximumWindowSize;
  353.                 }
  354.  
  355.                 [DllImport( "kernel32.dll", SetLastError = true )]
  356.                 private static extern bool ReadConsoleOutput( IntPtr hConsoleOutput, IntPtr lpBuffer, COORD dwBufferSize, COORD dwBufferCoord, ref SMALL_RECT lpReadRegion );
  357.  
  358.                 [DllImport( "kernel32.dll", SetLastError = true )]
  359.                 private static extern IntPtr GetStdHandle( int nStdHandle );
  360.  
  361.         }
  362.         #endregion Read From Console Buffer
  363. }
  364. //csharp/7347

Reply to "Capture the current screen and save it as a picture"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website