欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C#将当前屏幕抓取下来保存为图片的代码

程序员文章站 2022-04-09 09:16:06
...

在研发期间,将内容过程中比较好的内容记录起来,下边资料是关于C#将当前屏幕抓取下来保存为图片的内容,应该是对各朋友有较大用。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace RobvanderWoude
{
	class PrintScreen
	{
		static int Main( string[] args )
		{
			try
			{
				string output = string.Empty;
				bool overwrite = false;
				bool text = false;
				ImageFormat type = null;

				#region Command Line parsing

				if ( args.Length == 0 )
				{
					return WriteError( );
				}

				foreach ( string arg in args )
				{
					switch ( arg.ToUpper( ).Substring( 0, 2 ) )
					{
						case "/?":
							return WriteError( );
						case "/O":
							overwrite = true;
							break;
						case "/T":
							if ( text )
							{
								return WriteError( "Cannot capture current window as bitmap" );
							}
							switch ( arg.ToUpper( ).Substring( 3 ) )
							{
								case "BMP":
									type = ImageFormat.Bmp;
									break;
								case "GIF":
									type = ImageFormat.Gif;
									break;
								case "JPG":
								case "JPEG":
									type = ImageFormat.Jpeg;
									break;
								case "PNG":
									type = ImageFormat.Png;
									break;
								case "TIF":
								case "TIFF":
									type = ImageFormat.Tiff;
									break;
								case "TXT":
									text = true;
									break;
								default:
									return WriteError( "Invalid file format: "" + arg.Substring( 4 ) + """ );
							}
							break;
						default:
							output = arg;
							break;
					}
				}

				if ( !Directory.Exists( Path.GetDirectoryName( output ) ) )
				{
					return WriteError( "Invalid path for output file: "" + output + """ );
				}

				if ( File.Exists( output ) )
				{
					if ( overwrite )
					{
						File.Delete( output );
					}
					else
					{
						return WriteError( "File exists; use /O to overwrite existing files." );
					}
				}

				if ( type == null && text == false )
				{
					string ext = Path.GetExtension( output ).ToUpper( );
					switch ( ext )
					{
						case ".BMP":
							type = ImageFormat.Bmp;
							break;
						case ".GIF":
							type = ImageFormat.Gif;
							break;
						case ".JPG":
						case ".JPEG":
							type = ImageFormat.Jpeg;
							break;
						case ".PNG":
							type = ImageFormat.Png;
							break;
						case ".TIF":
						case ".TIFF":
							type = ImageFormat.Tiff;
							break;
						case ".TXT":
							text = true;
							break;
						default:
							return WriteError( "Invalid file type: "" + ext + """ );
							return 1;
					}
				}

				#endregion Command Line parsing

				if ( text )
				{
					string readtext = string.Empty;
					for ( short i = 0; i < (short) Console.BufferHeight; i++ )
					{
						foreach ( string line in ConsoleReader.ReadFromBuffer( 0, i, (short) Console.BufferWidth, 1 ) )
						{
							readtext += line + "n";
						}
					}
					StreamWriter file = new StreamWriter( output );
					file.Write( readtext );
					file.Close( );
				}
				else
				{
					int width = Screen.PrimaryScreen.Bounds.Width;
					int height = Screen.PrimaryScreen.Bounds.Height;
					int top = 0;
					int left = 0;
					Bitmap printscreen = new Bitmap( width, height );
					Graphics graphics = Graphics.FromImage( printscreen as Image );
					graphics.CopyFromScreen( top, left, 0, 0, printscreen.Size );
					printscreen.Save( output, type );
				}
				return 0;
			}
			catch ( Exception e )
			{
				Console.Error.WriteLine( e.Message );
				return 1;
			}
		}
		#region Error Handling

		public static int WriteError( string errorMessage = "" )
		{
			PrintScreen,  Version 1.00
			Save a screenshot as image or save the current console buffer as text

			Usage:   PRINTSCREEN  outputfile  [ /T:type ]  [ /O ]

			Where:   outputfile   is the file to save the screenshot or text to
			         /T:type      specifies the file type: BMP, GIF, JPG, PNG, TIF or TXT
			                      (only required if outputfile extension is different)
			         /O           overwrites an existing file
			
			
			Written by Rob van der Woude

			Console.ResetColor( );
			if ( string.IsNullOrEmpty( errorMessage ) == false )
			{
				Console.Error.WriteLine( );
				Console.ForegroundColor = ConsoleColor.Red;
				Console.Error.Write( "ERROR:  " );
				Console.ForegroundColor = ConsoleColor.White;
				Console.Error.WriteLine( errorMessage );
				Console.ResetColor( );
			}
			Console.Error.WriteLine( );
			Console.Error.WriteLine( "PrintScreen,  Version 1.10" );
			Console.Error.WriteLine( "Save a screenshot as image or save the current console buffer as text" );
			Console.Error.WriteLine( );
			Console.Error.Write( "Usage:   " );
			Console.ForegroundColor = ConsoleColor.White;
			Console.Error.WriteLine( "PRINTSCREEN  outputfile  [ /T:type ]  [ /O ]" );
			Console.ResetColor( );
			Console.Error.WriteLine( );
			Console.Error.Write( "Where:   " );
			Console.ForegroundColor = ConsoleColor.White;
			Console.Error.Write( "outputfile" );
			Console.ResetColor( );
			Console.Error.WriteLine( "   is the file to save the screenshot or text to" );
			Console.ForegroundColor = ConsoleColor.White;
			Console.Error.Write( "         /T:type" );
			Console.ResetColor( );
			Console.Error.Write( "      specifies the file type: " );
			Console.ForegroundColor = ConsoleColor.White;
			Console.Error.Write( "BMP" );
			Console.ResetColor( );
			Console.Error.Write( ", " );
			Console.ForegroundColor = ConsoleColor.White;
			Console.Error.Write( "GIF" );
			Console.ResetColor( );
			Console.Error.Write( ", " );
			Console.ForegroundColor = ConsoleColor.White;
			Console.Error.Write( "JPG" );
			Console.ResetColor( );
			Console.Error.Write( ", " );
			Console.ForegroundColor = ConsoleColor.White;
			Console.Error.Write( "PNG" );
			Console.ResetColor( );
			Console.Error.Write( ", " );
			Console.ForegroundColor = ConsoleColor.White;
			Console.Error.Write( "TIF" );
			Console.ResetColor( );
			Console.Error.Write( " or " );
			Console.ForegroundColor = ConsoleColor.White;
			Console.Error.WriteLine( "TXT" );
			Console.ResetColor( );
			Console.Error.Write( "                      (only required if " );
			Console.ForegroundColor = ConsoleColor.White;
			Console.Error.Write( "outputfile" );
			Console.ResetColor( );
			Console.Error.WriteLine( " extension is different)" );
			Console.ForegroundColor = ConsoleColor.White;
			Console.Error.Write( "         /O" );
			Console.ResetColor( );
			Console.Error.WriteLine( "           overwrites an existing file" );
			Console.Error.WriteLine( );
			Console.Error.Write( "Credits: Code to read console buffer by Simon Mourier " );
			Console.ForegroundColor = ConsoleColor.DarkGray;
			Console.ResetColor( );
			Console.Error.Write( "         Code for graphic screenshot by Ali Hamdar    " );
			Console.ForegroundColor = ConsoleColor.DarkGray;
			Console.ResetColor( );
			Console.Error.WriteLine( );
			Console.Error.WriteLine( "Written by Rob van der Woude" );
			return 1;
		}
		#endregion Error Handling

	}

	#region Read From Console Buffer

	public class ConsoleReader
	{
		public static IEnumerable<string> ReadFromBuffer( short x, short y, short width, short height )
		{
			if ( buffer == null )
				throw new OutOfMemoryException( );

			try
			{
				COORD coord = new COORD( );
				SMALL_RECT rc = new SMALL_RECT( );
				rc.Left = x;
				rc.Top = y;
				rc.Right = (short) ( x + width - 1 );
				rc.Bottom = (short) ( y + height - 1 );

				COORD size = new COORD( );
				size.X = width;
				size.Y = height;

				const int STD_OUTPUT_HANDLE = -11;
				if ( !ReadConsoleOutput( GetStdHandle( STD_OUTPUT_HANDLE ), buffer, size, coord, ref rc ) )
				{
					throw new Win32Exception( Marshal.GetLastWin32Error( ) );
				}

				IntPtr ptr = buffer;
				for ( int h = 0; h < height; h++ )
				{
					StringBuilder sb = new StringBuilder( );
					for ( int w = 0; w < width; w++ )
					{
						CHAR_INFO ci = (CHAR_INFO) Marshal.PtrToStructure( ptr, typeof( CHAR_INFO ) );
						char[] chars = Console.OutputEncoding.GetChars( ci.charData );
						sb.Append( chars[0] );
						ptr += Marshal.SizeOf( typeof( CHAR_INFO ) );
					}
					yield return sb.ToString( );
				}
			}
			finally
			{
				Marshal.FreeHGlobal( buffer );
			}
		}

		[StructLayout( LayoutKind.Sequential )]
		private struct CHAR_INFO
		{
			[MarshalAs( UnmanagedType.ByValArray, SizeConst = 2 )]
			public byte[] charData;
			public short attributes;
		}

		[StructLayout( LayoutKind.Sequential )]
		private struct COORD
		{
			public short X;
			public short Y;
		}

		[StructLayout( LayoutKind.Sequential )]
		private struct SMALL_RECT
		{
			public short Left;
			public short Top;
			public short Right;
			public short Bottom;
		}

		[StructLayout( LayoutKind.Sequential )]
		private struct CONSOLE_SCREEN_BUFFER_INFO
		{
			public COORD dwSize;
			public COORD dwCursorPosition;
			public short wAttributes;
			public SMALL_RECT srWindow;
			public COORD dwMaximumWindowSize;
		}

		[DllImport( "kernel32.dll", SetLastError = true )]
		private static extern bool ReadConsoleOutput( IntPtr hConsoleOutput, IntPtr lpBuffer, COORD dwBufferSize, COORD dwBufferCoord, ref SMALL_RECT lpReadRegion );

		[DllImport( "kernel32.dll", SetLastError = true )]
		private static extern IntPtr GetStdHandle( int nStdHandle );

	}
	#endregion Read From Console Buffer
}
相关标签: 图片处理