欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#抓取當(dāng)前屏幕并保存為圖片的方法

 更新時間:2015年04月17日 12:36:23   作者:work24  
這篇文章主要介紹了C#抓取當(dāng)前屏幕并保存為圖片的方法,實例分析了C#操作圖片的相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下

本文實例講述了C#抓取當(dāng)前屏幕并保存為圖片的方法。分享給大家供大家參考。具體分析如下:

這是一個C#實現(xiàn)的屏幕抓取程序,可以抓取整個屏幕保存為指定格式的圖片,并且保存當(dāng)前控制臺緩存到文本

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;
     }
    }
    // Check if directory exists
    if ( !Directory.Exists( Path.GetDirectoryName( output ) ) )
    {
     return WriteError( "Invalid path for output file: \"" + output + "\"" );
    }
    // Check if file exists, and if so, if it can be overwritten
    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 = "" )
  {
   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.Error.WriteLine( "http://www.sina.com.cn" );
   Console.ResetColor( );
   Console.Error.Write( "   Code for graphic screenshot by Ali Hamdar " );
   Console.ForegroundColor = ConsoleColor.DarkGray;
   Console.Error.WriteLine( "http://www.dbjr.com.cn" );
   Console.ResetColor( );
   Console.Error.WriteLine( );
   Console.Error.WriteLine( "Written by Rob van der Woude" );
   Console.Error.WriteLine( "http://www.qq.com" );
   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 )
  {
   IntPtr buffer = Marshal.AllocHGlobal( width * height * Marshal.SizeOf( typeof( CHAR_INFO ) ) );
   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 ) )
    {
     // 'Not enough storage is available to process this command' may be raised for buffer size > 64K (see ReadConsoleOutput doc.)
     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
}

希望本文所述對大家的C#程序設(shè)計有所幫助。

相關(guān)文章

  • 如何清空文件夾里面的所有文件和文件夾

    如何清空文件夾里面的所有文件和文件夾

    以下是對c#中清空文件夾里面的所有文件和文件夾的實現(xiàn)代碼進行了詳細的分析介紹,需要的朋友可以參考下
    2013-07-07
  • C#生成隨機驗證碼代碼分享

    C#生成隨機驗證碼代碼分享

    這篇文章主要分享了C#生成隨機驗證碼代碼,另外附上使用示例,非常的簡單實用,有需要的朋友可以參考下
    2014-10-10
  • C#難點逐個擊破(7):checked與unchecked

    C#難點逐個擊破(7):checked與unchecked

    checked 關(guān)鍵字用于對整型算術(shù)運算和轉(zhuǎn)換顯式啟用溢出檢查。
    2010-02-02
  • C#實現(xiàn)批量更改文件名稱大小寫或擴展名

    C#實現(xiàn)批量更改文件名稱大小寫或擴展名

    這篇文章主要為大家詳細介紹了如何利用C#實現(xiàn)批量更改文件名稱大小寫或擴展名的功能,文中的示例代碼講解詳細,對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
    2022-12-12
  • C#基于時間輪調(diào)度實現(xiàn)延遲任務(wù)詳解

    C#基于時間輪調(diào)度實現(xiàn)延遲任務(wù)詳解

    在很多.net開發(fā)體系中開發(fā)者在面對調(diào)度作業(yè)需求的時候一般會選擇三方開源成熟的作業(yè)調(diào)度框架來滿足業(yè)務(wù)需求,但是有些時候可能我們只是需要一個簡易的延遲任務(wù)。本文主要分享一個簡易的基于時間輪調(diào)度的延遲任務(wù)實現(xiàn),需要的可以參考一下
    2022-12-12
  • Entity Framework主從表的增刪改

    Entity Framework主從表的增刪改

    這篇文章介紹了Entity Framework主從表的增刪改,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • C# WinForm實現(xiàn)自動更新程序之客戶端的示例代碼

    C# WinForm實現(xiàn)自動更新程序之客戶端的示例代碼

    這篇文章主要為大家詳細介紹了利用C# WinForm實現(xiàn)自動更新程序之客戶端的實現(xiàn)方法,文中的示例代碼講解詳細,感興趣的小伙伴可以嘗試一下
    2022-10-10
  • 如何在C#?枚舉中增加行為

    如何在C#?枚舉中增加行為

    這篇文章主要介紹了如何在C#?枚舉中增加行為的相關(guān)資料,需要的朋友可以參考下
    2023-05-05
  • C#生成隨機ArrayList的方法

    C#生成隨機ArrayList的方法

    這篇文章主要介紹了C#生成隨機ArrayList的方法,實例分析了C#中ArrayList的相關(guān)操作技巧,需要的朋友可以參考下
    2015-06-06
  • Unity Shader實現(xiàn)水墨效果

    Unity Shader實現(xiàn)水墨效果

    這篇文章主要為大家詳細介紹了Unity Shader實現(xiàn)水墨效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04

最新評論