C # screen capture

Source: Internet
Author: User
Turn: http://blog.csdn.net/jxufewbt/archive/2007/07/08/1682832.aspx

To complete this function, you must first understand how to call an API (ApplicationProgramInterface. Although many class libraries have been provided in the. NET Framework, these class libraries are also very powerful, but for some underlying windows programming, you still need to call these API functions. All APIs are run in the "kernel", "user", and "GDI" libraries. For example, "kernel" and its library name is "kernel32.dll ", it is mainly used to generate associations with the operating system, such as program loading, Context Selection, file input and output, and memory management. "User" is named "user32.dll" in Win32 ". It allows you to manage all user interfaces. For example, window, menu, dialog box, icon, etc. "GDI" (image device interface), the Library name in Win32 is: "gdi32.dll", which is the image output library. Use GDI windows to "Draw" a window, menu, and dialog box. It can create a graphical output, and it can also save graphical files. As this article involves image problems, all class libraries called are "gdi32.dll ". In this program, the API function we use is "bitblt". This function is certainly not unfamiliar to many programmers, because it is widely used in image processing, you often have to deal with other programming languages. There is a namespace in the. NET Framework SDK.
"System. runtime. interopservices", this namespace provides a series of classes to access COM objects and call local API functions. The following is a declaration of this function in C:

[System. runtime. interopservices. dllimportattribute ("gdi32.dll")]
Private Static extern bool bitblt (
Intptr hdcdest, // handle of the Target DC
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
);

Through the above statement, you canCode.

The following describes how to implement a screen capture program using C:

(1) first, obtain the graphic object of the current screen. You can use the following code:

Graphics G1 = This. creategraphics ();

(2) create a bitmap object, and the size of this bitmap object is the current screen:

First, you need to obtain the current screen size, which can be achieved through the getworkingarea () method of the "screen" Class in the namespace "system. Windows. Forms. The following shows the length and width of the current screen ):

Rectangle rect = new rectangle ();
Rect = screen. getworkingarea (this );
"Screen width" = rect. width;
"Screen length" = rect. height;

Now we can get the desired bitmap, which can be achieved through the following statements:

Image myimage = new Bitmap (rect. Width, rect. Height, G1 );
// Create a bitmap with the screen size as the standard

(3). Obtain the DC of the current screen and the bitmap object, which can be achieved through the following statement:

// Obtain the DC of the screen
Intptr DC1 = g1.gethdc ();
// Obtain the bitmap DC
Intptr DC2 = g2.gethdc ();

(4) Call the API function to copy the current screen to the created bitmap:

Bitblt (DC2, 0, 0, rect. Width, rect. Height, DC1, 0, 0, 13369376 );

(5) release the current screen and the DC of this bitmap object. The following code can be used:

// Release the DC of the screen
G1.releasehdc (DC1 );
// Release the bitmap DC
G2.releasehdc (DC2 );

(6) Save the bitmap object to form a jpg image:

Myimage. Save (@ "C: \ capture.jpg", imageformat. JPEG );

Of course, you can also save the screen in the format of other images as needed. If you want to save the image as a bitmap file, you can set "imageformat. change jpeg to imageformat. BMP "; to save the image as a GIF file, set" imageformat. change jpeg to imageformat. GIF ". There are about a dozen types of files you can save. I will not describe them here. Of course, you need to change the suffix of the file to be saved.

Use C # To capture the source code of the screen (capture. CS ):

After understanding the implementation methods of the above steps, you can get the source program that uses C # To capture the screen, as shown below:

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 ()
{
// Initialize components in the form
Initializecomponent ();
}
// Clear 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 current screen! ";
Resumelayout (false );

}
// Declare an API Function
[System. runtime. interopservices. dllimportattribute ("gdi32.dll")]
Private Static extern bool bitblt (
Intptr hdcdest, // handle of the Target DC
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 current screen as the template
Graphics G1 = This. creategraphics ();
// Create a bitmap with the screen size as the standard
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 this API function for screen capture
Bitblt (DC2, 0, 0, rect. Width, rect. Height, DC1, 0, 0, 13369376 );
// Release the DC of the screen
G1.releasehdc (DC1 );
// Release the bitmap DC
G2.releasehdc (DC2 );
// Save as JPG
Myimage. Save (@ "C: \ capture.jpg", imageformat. JPEG );
MessageBox. Show ("the front screen is saved as a C-drive capture.jpg file! ");
}
}

Related Article

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.