Resolution C to capture the current program window specified location screenshot of the implementation of the method

  • 2020-05-12 03:04:56
  • OfStack

To do this, learn how to call the API function in C #. Although many libraries,400 phones, have been reduced in the.Net framework, and these libraries are very powerful, the top-level programming of some Windows is still achieved by calling these API functions. 1 cut API are all in the "Kernel", "User "and "GDI" libraries to operate: "Kernel", his library name is" KERNEL32.DLL ", it is important for the generation of operations between the system relations, such as: program loading, context selection, file input and output, memory management and so on. User "this class library is named" USER32.DLL "in Win32. It allows the governance of all users to communicate. For example: window heart, menu, dialog box, picture book and so on." GDI (image equipment interface), it is in Win32 library is: "GDI32.dll", it is a graphics output library. Use GDI Windows to "draw" the heart of the window, dishes and dialog boxes; It can create and repair graphics output; It can also save graphics files. Because the text touches on the image problem, the class library called by 1 cut is "GDI32.dll". In the original program people use the API function is "BitBlt", this function about the broad programmer to come, must not feel unfamiliar, because his use in image processing is absolutely narrow, in the use of other program language programming, often have to talk with him. In.Net FrameWork SDK there is a literal space "System.Runtime.InteropServices", which subsets 1 series of classes to visit COM objects and call API functions in the field. Here's how to highlight this function in C # :

[ System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll" ) ]
private static extern bool BitBlt (
    IntPtr hdcDest , //  purpose  DC The handle of 
    int nXDest ,
    int nYDest ,
    int nWidth ,
    int nHeight ,
    IntPtr hdcSrc , //  flow DC The handle of 
    int nXSrc ,
    int nYSrc ,
    System.Int32 dwRop //  Grating disposal value 
) ;

With the above sound, you can use the function in the code below.
Below are the detailed true-to-life steps for creating a screen capture program using C # :
(1). First, the graphic object of the current screen should be obtained. It can be hidden through the following code:
Graphics g1 = this.CreateGraphics ( ) ;
(2). Create 1 Bitmap object, October mummy, and the size of that Bitmap object is the current screen:
The first step is to get the size of the current screen, which can be achieved by GetWorkingArea () of the "Screen" class in the "System.Windows.Forms" real word space. Here is the length (Height) and width (Width) of the current screen:
Rectangle rect = new Rectangle ( ) ;
rect = Screen.GetWorkingArea ( this ) ;
"Screen width" = rect.Width;
"Screen length" = rect.Height;
The following sentence can be completed:
Image MyImage = new Bitmap ( rect.Width , rect.Height , g1 ) ;
// create and repair bitmaps based on the size of the screen
(3). Get the current screen and this Bitmap for the DC of the image, which can be hidden by the following statements:
// lose the DC of the screen
IntPtr dc1 = g1.GetHdc ( ) ;
// lost Bitmap's DC
IntPtr dc2 = g2.GetHdc ( ) ;
(4). Call the API function and copy the screen into the Bitmap created:
BitBlt ( dc2 , 0 , 0 , rect.Width , rect.Height , dc1 , 0 , 0 , 13369376 ) ;
(5). To interpret the front screen and DC for the image, the following code can be completed:
// release DC from the screen
g1.ReleaseHdc ( dc1 ) ;
// release DC of Bitmap
g2.ReleaseHdc ( dc2 ) ;
(6). Keep Bitmap for the image, forming jpg picture:
MyImage.Save ( @"c:\Capture.jpg" , ImageFormat.Jpeg );
If you want to save the image as a bitmap file, you can replace "ImageFormat.Jpeg" with "ImageFormat.Bmp"; I want to keep the pictures as Gif, so I replace "ImageFormat.Jpeg" with "ImageFormat.Gif". There are about 10 different file types that you can save, so you don't have to go through each one, and of course you have to change the suffix quite a bit.
Use C # to capture the screen stream code (Capture.cs) :
By understanding how these steps are implemented, you can get a streaming program that captures the screen with C #, as follows:

using System ;
using System.Drawing ;
using System.Collections ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data ;
using System.Drawing.Imaging ;
public class Form1 : Form
{
    private Button button1 ;
    private System.ComponentModel.Container components = null ;
public Form1 ( )
{
    // Start the various components in the form 
    InitializeComponent ( ) ;
}
//  Clean up the resources used in the program 
protected override void Dispose ( bool disposing )
{
    if ( disposing )
    {
        if ( components != null )
        {
            components.Dispose ( ) ;
        }
    }
    base.Dispose ( disposing ) ;
}
private void InitializeComponent ( )
{
    button1 = new Button ( );
    SuspendLayout ( ) ;
    button1.Location = new System.Drawing.Point ( 64 , 40 ) ;
    button1.Name = "button1" ;
    button1.Size = new System.Drawing.Size ( 80 , 32 ) ;
    button1.TabIndex = 0 ;
    button1.Text = " capture " ;
    button1.Click += new System.EventHandler ( button1_Click ) ;
    AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ;
    ClientSize = new System.Drawing.Size ( 216 , 125 ) ;
    Controls.Add ( button1 ) ;
    MaximizeBox = false ;
    MinimizeBox = false ;
    Name = "Form1" ;
    Text = "C# Capture after screen! " ;
    ResumeLayout ( false ) ;
}
// The sound is bright 1 a API function 
    [ System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll" ) ]
    private static extern bool BitBlt (
        IntPtr hdcDest , //  purpose  DC The handle of 
        int nXDest ,
        int nYDest ,
        int nWidth ,
        int nHeight ,
        IntPtr hdcSrc , //  The source DC The handle of 
        int nXSrc ,
        int nYSrc ,
        System.Int32 dwRop //  Grating disposal value 
    ) ;
static void Main ( )
{
    Application.Run ( new Form1 ( ) ) ;
}
private void button1_Click ( object sender , System.EventArgs e )
{
    // Gets the current screen size 
    Rectangle rect = new Rectangle ( ) ;
    rect = Screen.GetWorkingArea ( this ) ;
    // founded 1 An image with a subsequent screen as a template 
    Graphics g1 = this.CreateGraphics ( ) ;
    // Create bitmaps in terms of screen size 
    Image MyImage = new Bitmap ( rect.Width , rect.Height , g1 ) ;
    Graphics g2 = Graphics.FromImage ( MyImage ) ;
    // Get the screen DC
    IntPtr dc1 = g1.GetHdc ( ) ;
    // get Bitmap the DC
    IntPtr dc2 = g2.GetHdc ( ) ;
    // Call 1 pet. API Function to complete screen capture 
    BitBlt ( dc2 , 0 , 0 , rect.Width , rect.Height , dc1 , 0 , 0 , 13369376 ) ;
    // Release the screen DC
    g1.ReleaseHdc ( dc1 ) ;
    // Walk off Bitmap the DC
    g2.ReleaseHdc ( dc2 ) ;
    // In order to JPG White pattern to keep 
    MyImage.Save ( @"c:\Capture.jpg" , ImageFormat.Jpeg );
    MessageBox.Show ( " The front screen has been retained as C The plate capture.jpg White dress! " ) ;
}
}

Related articles: