In fact, C # can be used to complete almost all functions of other programming languages. Of course, C #'s many unique functions cannot be implemented by other programming languages, this is why C # is more and more popular with programmers. This article will discuss how to use C # to implement an important function and use it to write a program to capture the current screen. Through this program, we will understand how C # Calls API functions and how rich class libraries in the. NET Framework are and how powerful the functions are.
I. programming and development and running environment:
(1). Microsoft Windows 2000 Server Edition
(2). Net Framework SDK beta 2
II. Key Steps of Program Design:
To complete this function, you must first understand how to call an API (application interface) function in C. 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. In the. NET Framework SDK, there is a namespace "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 declaration, you can use this function in the following code.
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.
3. 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! ");
}
}
Iv. Summary:
Although this is an application example of C # In actual situations, it contains a wealth of content. Calling Windows API functions is a headache for many programmers, because it involves a wide range of fields, not only do you have to understand the structure, type conversion, secure/Insecure code, managed/unmanaged code, but also the structure of API functions in C. I hope this article will help you.