Solution C # screenshot capture at the specified position in the current program window

Source: Internet
Author: User

To complete this function, you must first understand how to call the API (using program interfaces) function in C. Certainly. net Framework has been reduced to many class libraries, 400 phones, these class libraries are also very powerful, but for some top-level Windows programming, it is still possible to call these API functions. All APIs are run in the "Kernel", "User", and "GDI" Libraries: "Kernel", whose library name is "KERNEL32.DLL ", it is mainly used to take place the relationship between the system, 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 management of all users. For example: window, dish, dialog box, graph, etc. "GDI" (image equipment interface), which is in the Win32 library is actually: "GDI32.dll", it is the graphics output library. Use GDI Windows to "paint" the window, dish, and dialog box. It can create and repair image output, and it can also save graphic files. Because the original article involves image answer, all class libraries called are "GDI32.dll ". In the original program, the API function used by people is "BitBlt". This function is about the wide programmer, and he must not feel unfamiliar, because his use in image processing is absolutely narrow, in programming with other programs, we often have to talk to him. In the. Net FrameWork SDK, there is a real-name space "System. Runtime. InteropServices". This namespace is downgraded to a series of classes to visit COM objects and call API functions from other places. The function is highlighted in C # as follows:
Copy codeThe Code is as follows: [System. Runtime. InteropServices. DllImportAttribute ("gdi32.dll")]
Private static extern bool BitBlt (
IntPtr hdcDest, // The destination DC handle
Int nXDest,
Int nYDest,
Int nWidth,
Int nHeight,
IntPtr hdcSrc, // stream DC handle
Int nXSrc,
Int nYSrc,
System. Int32 dwRop // The processing value of the grating
);

With the above voice, you can use this function in the code below.
Below are the detailed hidden steps for using C # As the screen capture program:
(1) first, obtain the graphic object of the current screen. You can use the following code to hide it:
Graphics g1 = this. CreateGraphics ();
(2) create a Bitmap object, Mommy, October, and the size of the Bitmap object is the current screen:
First, you must obtain the size of the current Screen, which can be achieved through the GetWorkingArea () method of the "Screen" Class in the real-name space "System. Windows. Forms. The following shows the length (Height) and Width of the current screen ):
Rectangle rect = new Rectangle ();
Rect = Screen. GetWorkingArea (this );
"Wide screen" = rect. Width;
"Screen length" = rect. Height;
He will be able to lose the Bitmap that people want to read, and can do it through the following statements:
Image MyImage = new Bitmap (rect. Width, rect. Height, g1 );
// Creates a bitmap based on the screen size.
(3). To obtain the DC of the current screen and the Bitmap image, you can use the following statements to hide them:
// DC without screen loss
IntPtr dc1 = g1.GetHdc ();
// The Bitmap DC is lost.
IntPtr dc2 = g2.GetHdc ();
(4) Call the API function to copy the screen to the created Bitmap:
BitBlt (dc2, 0, 0, rect. Width, rect. Height, dc1, 0, 0, 13369376 );
(5). Release the front screen and Bitmap for the image's DC. The code can be completed through the following work:
// Unlock the DC of the screen
G1.ReleaseHdc (dc1 );
// Release the Bitmap DC
G2.ReleaseHdc (dc2 );
(6) Keep the Bitmap object to form a jpg image:
MyImage. Save (@ "c: \ Capture.jpg", ImageFormat. Jpeg );
You can also save the screen as a bitmap file based on your needs. change Jpeg to ImageFormat. bmp "; Save the image as a Gif white, and then" ImageFormat. change Jpeg to ImageFormat. gif ". There are about 10 types of files that you can save, and they are not introduced one by one. Of course, you must change the suffix of the files that you want to retain.
Use C # To Capture the screen's stream program code (Capture. cs ):
After understanding the implementation of the above steps, you can get the stream program that uses C # To capture the screen, as shown below:Copy codeThe Code is 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 ()
{
// Each component in the initial form
InitializeComponent ();
}
// Clear 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 (button#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 the screen later! ";
ResumeLayout (false );
}
// Declare an API Function
[System. Runtime. InteropServices. DllImportAttribute ("gdi32.dll")]
Private static extern bool BitBlt (
IntPtr hdcDest, // The destination DC handle
Int nXDest,
Int nYDest,
Int nWidth,
Int nHeight,
IntPtr hdcSrc, // source DC handle
Int nXSrc,
Int nYSrc,
System. Int32 dwRop // The processing value of the grating
);
Static void Main ()
{
Application. Run (new Form1 ());
}
Private void button#click (object sender, System. EventArgs e)
{
// Obtain the current screen size
Rectangle rect = new Rectangle ();
Rect = Screen. GetWorkingArea (this );
// Create an image with the following screen as the template
Graphics g1 = this. CreateGraphics ();
// Create a bitmap based on the screen size
Image MyImage = new Bitmap (rect. Width, rect. Height, g1 );
Graphics g2 = Graphics. FromImage (MyImage );
// Obtain the DC of the screen
IntPtr dc1 = g1.GetHdc ();
// Obtain the Bitmap DC
IntPtr dc2 = g2.GetHdc ();
// Call the API function to capture the screen
BitBlt (dc2, 0, 0, rect. Width, rect. Height, dc1, 0, 0, 13369376 );
// Unlock the DC of the screen
G1.ReleaseHdc (dc1 );
// Release the Bitmap DC
G2.ReleaseHdc (dc2 );
// Retain the image in JPG white space
MyImage. Save (@ "c: \ Capture.jpg", ImageFormat. Jpeg );
MessageBox. Show ("the front screen has been reserved as a C-drive capture.jpg white file! ");
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.