We have learned how Visual Basic, Delphi, and other languages can capture screen images. So how does C # implement this function? This article will discuss this issue.
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 and specific implementation methods:
(1) first create a bitmap object with the same size as the current screen:
To implement this operation, you must first obtain the DC of the current display, then create a graphic object based on the DC, and then generate this bitmap object based on the graphic object. The generated bitmap object is the same as the current screen size. To obtain the DC of the monitor, you cannot use the. NET class library. Therefore, you need to call a Windows API function. We know that all windows APIs are encapsulated in files in the "kernel", "user", and "GDI" Libraries: "kernel", whose library name is "kernel32.dll ". "User" is named "user32.dll" in Win32 ". It manages all user interfaces. For example, window, menu, dialog box, icon, etc. "GDI" (image device interface), whose database name in Win32 is: "gdi32.dll", to obtain the DC of the display, the called API function-createdc (), is encapsulated in this class library. To declare the Windows API function in C #, you need to use it.. NET Framework SDK 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 intptr createdc ( String lpszdriver, // driver name String lpszdevice, // device name String lpszoutput, // useless, can be set to "null" Intptr lpinitdata // any printer data ); After declaring this API function in C #, you can create a bitmap object with the same size as the display. The specific implementation statement is as follows: Intptr DC1 = createdc ("display", null, null, (intptr) null ); // Create a DC for the monitor Graphics G1 = graphics. fromhdc (DC1 ); // Create a new graphics object by the handle of a specified device Myimage = new Bitmap (screen. primaryscreen. bounds. Width, screen. primaryscreen. bounds. Height, G1 ); // Create a bitmap object of the same size as the screen size |
(2) create a graphic object similar to this bitmap:
The following code implements this function:
Graphics g2 = graphics. fromimage (myimage ); |
(3). Obtain the handle of the current screen and bitmap:
To capture the current screen image in the next step, obtain the handle of the two objects. The specific capture method in the program is to capture the current screen to the created bitmap object. The specific implementation code is as follows:
// Obtain the screen handle Intptr DC3 = g1.gethdc (); // Obtain the bitmap handle Intptr DC2 = g2.gethdc (); // Capture the current screen in the image object |
(4) capture the current screen:
We implement this by saving the current screen to the created bitmap object. The specific implementation process is through bitblt, a Windows API function. I think most programmers must be familiar with this API function, because in image programming in windows, this function is used in many places. This API function is also encapsulated in "gdi32.dll" like the API function described above. The declaration of this function in C # is as follows:
[System. runtime. interopservices. dllimportattribute ("gdi32.dll")] Private Static extern bool bitblt ( Intptr hdcdest, // handle of the target device Int nxdest, // X coordinate in the upper left corner of the target object Int nydest, // X coordinate in the upper left corner of the target object Int nwidth, // The width of the rectangle of the target object Int nheight, // The length of the rectangle of the target object Intptr hdcsrc, // Source Device handle Int nxsrc, // X coordinate in the upper left corner of the source object Int nysrc, // X coordinate in the upper left corner of the source object System. int32 dwdrop // The operation value of the grating ); With this declaration, you can save the current screen as follows: Bitblt (DC2, 0, 0, screen. primaryscreen. bounds. Width, screen. primaryscreen. bounds. Height, DC3, 0, 0, 13369376 ); |
(5) Save the current screen to the hard disk and release the handle:
G1.releasehdc (DC3 ); // Release the screen handle G2.releasehdc (DC2 ); // Release the bitmap handle Myimage. Save ("C: // mydomain.jpg", imageformat. JPEG ); |
We can save the current screen in different file formats according to our requirements. The program described in this article is saved as a "jpg" file, you can change the file type saved to the hard disk by modifying the second parameter of the "save" method. For example, if the second parameter is "imageformat. GIF, then the file you saved to the hard disk is "GIF. For details about other file formats, see. NET Framework SDK.
3. Use C # to code and run the screen capture program:
After you have mastered these important steps, you can get the source code (capture. CS) of the screen capture program using C #. The details are as follows:
Using system; Using system. drawing; Using system. collections; Using system. componentmodel; Using system. Windows. forms; Using system. Data; Using system. Drawing. imaging; Using system. IO; // The namespace used for import in the program Public class Capture: Form { Private system. componentmodel. Container components = NULL; Private icon mnettrayicon = new icon ("tray. ICO "); Private bitmap myimage = NULL; Private policyicon trayicon; Private contextmenu policyiconmnu; Public capture () { // Initialize the components used in the form Initializecomponent (); }Protected override void onactivated (eventargs E) { This. Hide (); } [System. runtime. interopservices. dllimportattribute ("gdi32.dll")] Private Static extern bool bitblt ( Intptr hdcdest, // handle of the target device Int nxdest, // X coordinate in the upper left corner of the target object Int nydest, // X coordinate in the upper left corner of the target object Int nwidth, // The width of the rectangle of the target object Int nheight, // The length of the rectangle of the target object Intptr hdcsrc, // Source Device handle Int nxsrc, // X coordinate in the upper left corner of the source object Int nysrc, // X coordinate in the upper left corner of the source object System. int32 dwdrop // The operation value of the grating ); [System. runtime. interopservices. dllimportattribute ("gdi32.dll")] Private Static extern intptr createdc ( String lpszdriver, // driver name String lpszdevice, // device name String lpszoutput, // useless, can be set to "null" Intptr lpinitdata // any printer data ); Public void capture (Object sender, system. eventargs E) { This. Visible = false; Intptr DC1 = createdc ("display", null, null, (intptr) null ); // Create a DC for the monitor Graphics G1 = graphics. fromhdc (DC1 ); // Create a new graphics object by the handle of a specified device Myimage = new Bitmap (screen. primaryscreen. bounds. Width, screen. primaryscreen. bounds. Height, G1 ); // Create a bitmap object of the same size as the screen size Graphics g2 = graphics. fromimage (myimage ); // Obtain the screen handle Intptr DC3 = g1.gethdc (); // Obtain the bitmap handle Intptr DC2 = g2.gethdc (); // Capture the current screen in the image object Bitblt (DC2, 0, 0, screen. primaryscreen. bounds. Width, screen. primaryscreen. bounds. Height, DC3, 0, 0, 13369376 ); // Copy the current screen to the image G1.releasehdc (DC3 ); // Release the screen handle G2.releasehdc (DC2 ); // Release the bitmap handle Myimage. Save ("C: // mydomain.jpg", imageformat. JPEG ); MessageBox. Show ("the current screen has been saved to the C: // my).jpg file! "); This. Visible = true; } Public void exitselect (Object sender, system. eventargs E) { // Hide the icon in the pallet Program Trayicon. Visible = false; // Shut down the system This. Close (); } // Clear resources used in the program Public override void dispose () { Base. Dispose (); If (components! = NULL) Components. Dispose (); } Private void initializecomponent () { // Set attributes of the pallet Program Trayicon = new notifyicon (); Trayicon. Icon = mnettrayicon; Trayicon. Text = "use C # As the screen capture program "; Trayicon. Visible = true; // Define a menuitem array and assign the array to the contextmenu object at the same time Menuitem [] mnuitms = new menuitem [3]; Mnuitms [0] = new menuitem (); Mnuitms [0]. Text = "capture the current screen! "; Mnuitms [0]. Click + = new system. eventhandler (this. Capture ); Mnuitms [1] = new menuitem ("-"); Mnuitms [2] = new menuitem (); Mnuitms [2]. Text = "exit system "; Mnuitms [2]. Click + = new system. eventhandler (this. exitselect ); Mnuitms [2]. defaultitem = true; Policyiconmnu = new contextmenu (mnuitms ); Trayicon. contextmenu = policyiconmnu; // Add the set contextmenu object to the tray Program This. suspendlayout (); This. autoscalebasesize = new system. Drawing. Size (5, 13 ); This. clientsize = new system. Drawing. Size (320, 56 ); This. controlbox = false; This. maximizebox = false; This. minimizebox = false; This. windowstate = system. Windows. Forms. formwindowstate. minimized; This. Name = "capture "; This. showintaskbar = false; This. Text = "use C # As the screen capture program! "; This. resumelayout (false ); } Static void main () { Application. Run (New capture ()); } } |
Iv. Summary:
Although. the. NET Framework SDK is rich in content and has powerful functions. However, some underlying operations can be implemented only by using Windows API functions, the key to implementing screen capture is to know how to call API functions in C. I hope this article will help you with API programming in C.