We have already understood how languages such as Visual Basic or Delphi is captured to the screen image. So how do you implement this feature for C #? This article will discuss this issue. One. Program design development and operating environment: (1). Microsoft Window 2000 Server Edition (2) .. Net Framework SDK Beta 2. Programming key steps and specific implementation methods: (1). First to create a Bitmap object that is the same as the current screen size: To implement this, first get the DC of the current display, then create Graphic according to this DC Object, then this bitmap object is generated by this Graphic object. This generated bitmap object is consistent with the current screen size. Since the DC of the display is to be obtained, it is impossible to use the .NET class library, this requires calling a Windows API function. We know that all APIs in Windows are encapsulated in "Kernel", "User" and "GDI" three libraries in the three libraries: "Kernel", his library name is "kernel32.dll". "User" library is called "user32.dll" in Win32. It mainly manages all user interfaces. Such as: windows, menus, dialogs, icons, etc. "GDI" (image device interface), its library in Win32 is named: "GDI32.DLL", to get the display of the DC, the API function called "CREATEDC () is packaged in this class . To declare the API function of the window in C #, you need to use the namespace "System.Runtime.interopServices" in the .NET Framework SDK, this name space provides a series of classes to access the COM object, and call the local API function.
The following is a declaration in C # function: [System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll")] private static extern IntPtr CreateDC (string lpszDriver, // driver name string lpszDevice, // device name string lpszOutput, / / Useless, you can set "NULL" INTPTR LPINITDATA // Any printer data); declare this API function in C #, you can create a bitmap object that is consistent with the display size, the specific implementation statement is as follows: INTPTR DC1 = CreateDC ("Display", NULL, NULL, (INTPTR) NULL); // Create DCGraphics G1 = graphics.fromhdc (DC1) of the display; // Create a new Graphics object by a handle of a specified device MyImage = New Bitmap Screen.primaryScreen.Bounds.Width, Screen.primaryScreen.Bounds.Height, G1); // Create a Bitmap object (2) according to the screen size. Create a graphic object as the same as this bitmap: This feature can be implemented by the following code: Graphics G2 = graphics.fromimage (MyImage); (3). Handle of the current screen and bitmap: The handle of this two object is to the next step to the current screen image The specific capture method implemented in the program is to capture the current screen into the bitmap object that has been created. The specific implementation code is as follows: // Get the handle of the screen INTPTR DC3 = G1.GETHDC (); // Get the handle of the bitmap INTPTR DC2 = G2.GETHDC (); // Capture the current screen into the bitgraph object (4). Capture the current screen: We are implemented by the current screen to create a bitmap object, and the specific implementation is an API function -Bitblt in Windows. I think most of the programmers must be unfamiliar with this API function, because this function will be used in many places in the image programming of Windows.
This API function is the same as the API function introduced above, is also encapsulated in "gdi32.dll", the following is the declaration of this function in C #: [System.Runtime.InterOpServices.dllimportattribute ("gdi32.dll") ] Private static extern bool bitblt (INTPTR HDCDEST, / / Target device handle int nxdest, // target object of X coordinate int NYDEST, // target INT NWIDTH, / / target object Rectangular width int NHEIGHT, / / Target object's rectangular length INTPTR HDCSRC, // Source The handle of the handle INT NXSRC, // Source object The X coordinate INT NYSRC, // Source object's X coordinate system The operation value of the .INT32 dwrop // raster); I know this statement can be saved for the current screen. Specific 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 bitmap Handle MyImage.Save ("c: //myjpeg.jpg", imageformat.jpeg; we can save the current screen in different file formats according to your own requirements, and the program introduced in this article is "JPG" file Save, 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 save to the hard disk is "GIF "File. For other file formats, you can refer to the .NET Framework SDK, there is a detailed introduction.
three. Use C # to do Screen Capture program code and run program: After mastering these important steps, you can get the source code of the Screen Capture program with C #, as follows: use system; use system.drawing ; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Drawing.Imaging; using System.IO; // introduced into use in the program namespace public class Capture: form {private System.ComponentModel.Container components = null; private Icon mNetTrayIcon = new Icon ( "tray.ico"); private Bitmap MyImage = null; private NotifyIcon TrayIcon; private ContextMenu notifyiconMnu; public Capture () {// initialization form components used to InitializeComponent ();} protected override void OnActivated (EventArgs e) {this.Hide ();} [System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll")] private static extern bool BitBlt (IntPtr hdcDest , // Target device INT NXDEST, / / Target object's X coordinate int Nydest, // target object's X coordinate int NWIDTH, / / Target object's rectangular width int nHeiGHT, // Target The rectangular length of the object INTPTR HDCSRC, // Source device handle INT NXSRC, // Source object's X coordinate int NYSRC, // Source object's X coordinate system.int32 DWROP // raster operation value ) ; [System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll")] private static extern IntPtr CreateDC (string lpszDriver, // driver name string lpszDevice, // device name string lpszOutput, // useless, can set the bit "NULL "INTPTR LPINITDATA / / Arbitrary Printer Data); Public Void Capture (Object Sender, System.EventArgs E) {this.visible = false; INTPTR DC1 = Createdc (" Display ", NULL, NULL, (INTPTR) NULL); / / Create a display of Dcgraphics G1 = graphics.fromhdc (DC1); // Create a new Graphics object by a handle of a specified device MyImage = New Bitmap (Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, G1 ); Create a Bitmap object with the same size according to the screen size Graphics G2 =
Graphics.FromImage (MyImage); // Get the handle of the screen INTPTR DC3 = g1.GeTHDC (); // Get the handle of the bitmap INTPTR DC2 = G2.GETHDC (); // Capture the current screen into bitgraph object Bitblt ( DC2, 0, 0, Screen.PrimaryScreen.bounds.Width, Screen.primaryScreen.Bounds.Height, DC3, 0, 0, 13369376); // Copy the current screen into the bit map G1.RELEASEHDC (DC3); // Release Screen handle G2.ReleaseHDC (DC2); // Release bitmap sector myImage.save ("c: //myjpeg.jpg", imageformat.jpeg); MessageBox.show ("has saved the current screen to C: // myjpeg .jpg file! "); this.visible = true;} public void exitsLect (Object sender, system.eventargs e) {// Hide icon in the tray tray trayicon.visible = false; // Close system this.close ( );} // Clear the resource used in the program public override void dispose () {base.dispose (); if (Components! = Null) Components.dispose ();} private void initializationComponent () {// Set tray Programs of each attribute trayicon = new notifyicon (); trayicon.text = "Do Screen Capture program with C #"; TRAYICON.Visible = true; // Defines a MenuItem array and assigns this array to assign ContextMenu object 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; notifyiconMnu = new ContextMenu (mnuItms); TrayIcon.ContextMenu = notifyiconMnu; // the tray The program adds a set of ContextMenu object this.suspendlayout (); this.autoscalebasesize = new system.drawing.size (5, 13); this.clientsize = new system.drawing.size (320, 56); this.Controlbox = False; this.maximizebox =