Make C # Make screen capture programs

xiaoxiao2021-03-06  107

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

two. Programming key steps and specific implementation methods:

(1). First, create a Bitmap object that is the same as the current screen size:

To implement this, you must first get the DC of the current display, then create a Graphic object according to this DC, and 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. Below is declare this function in C #:

[System.Runtime.InteropServices.dllimportattribute ("gdi32.dll")]]]]

Private static extern INTPTR CREATEDC (

String lpszdriver, // Drive name

String lpszdevice, // device name

String lpszoutput, // is useless, you can set "NULL"

INTPTR LPINITDATA / / Arbitrary Printer Data

);

Declaring 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 a display of the DC

Graphics 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

(2) Create a graphic object as it is based on this bitmap:

This function can be implemented by the following code:

Graphics G2 = graphics.fromimage (MyImage); (3). Get the handle of the current screen and bitmap:

The handle of this two objects is for the next step to implement the capture of the current screen image, the specific capture of the program 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 a bitmap

INTPTR DC2 = G2.GETHDC ();

// capture the current screen into the bitmap object

(4). Capture the current screen:

We are implemented through the current screen to be implemented in the created 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, / / ​​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 Equipment Handle

INT NXSRC, // / 坐 坐 坐 in the upper left corner of the source

INT NYSRC, // 坐 坐 坐 坐 坐 坐

SYSTEM.INT32 DWROP / / Raster Operation Value

);

I know that this statement can be saved for 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 bitmap

MyImage.save ("c: //myjpeg.jpg", imageformat.jpeg;

We can save the current screen in a different file format according to your own requirements, and the program described in this article is saved in the "JPG" file, you can change it by modifying the second parameter of the "Save" method. The hard disk file type, 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 code and run programs for Screen Capture programs:

After mastering these important steps above, you can get the source code of the Screen Capture program with C #, which is as follows:

Using system;

Using system.drawing;

Using system.collections; using system.componentmodel;

Using system.windows.forms;

Using system.data;

Using system.drawing.image;

Using system.io;

// Import the namespace used 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 notifyicon trayicon;

Private contextmenu notifyiconmnu;

Public capture ()

{

// Initialize the components used in the form

InitializationComponent ();

}

Protected Override Void Onactivated (Eventargs E)

{

THIS.HIDE ();

}

[System.Runtime.InteropServices.dllimportattribute ("gdi32.dll")]]]]

Private static extern bool bitblt

INTPTR HDCDEST, / / ​​Target device handle

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 Equipment Handle

INT NXSRC, // / 坐 坐 坐 in the upper left corner of the source

INT NYSRC, // 坐 坐 坐 坐 坐 坐

SYSTEM.INT32 DWROP / / Raster Operation Value

);

[System.Runtime.InteropServices.dllimportattribute ("gdi32.dll")]]]]

Private static extern INTPTR CREATEDC (

String lpszdriver, // Drive name

String lpszdevice, // device name

String lpszoutput, // is useless, you can set "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 the DC

Graphics 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 a bitmap

INTPTR DC2 = G2.GETHDC (); // Capture the current screen into the bitmap 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 the screen handle

g2.releaseHDC (DC2);

// Release bitmap

MyImage.save ("c: //myjpeg.jpg", imageformat.jpeg;

Messagebox.show ("has already saved the current screen to the c: //myjpeg.jpg file!");

THIS.Visible = True;

}

Public Void EXITSELECT (Object Sender, System.EventArgs E)

{

// Hide icon in the tray program

TRAYICON.Visible = false;

// Turning the system

THIS.CLOSE ();

}

/ / Clear the resources used in the program

Public Override Void Dispose ()

{

Base.dispose ();

IF (Components! = NULL)

Components.dispose ();

}

Private vidinitiRizeComponent ()

{

/ / Set the various properties of the tray program

Trayicon = new notifyicon ();

Trayicon.icon = mnettrayicon;

TRAYICON.TEXT = "Do Screen Capture Program with C #";

TRAYICON.Visible = TRUE;

/ / Define a MenuItem array and assign this array to the ContextMenu object.

Menuitem [] mnuitms = new menuItem [3];

Mnuitms [0] = new menuitem ();

Mnuitms [0] .Text = "captures 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.exitslect);

Mnuitms [2] .defaultItem = true;

Notifyiconmnu = New ContextMenu (MNUITMS);

Trayicon.ContextMenu = Notifyiconmnu;

/ / Add a set CONTEXTMENU object for 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 # to do the Screen Capture program!";

This.ResumeLayout (false);

}

Static void main ()

{

Application.run (New Capture ());

}

}

The picture below is the run interface after this code:

Figure 01: Source code for Screen Capture programs with C #

four. to sum up:

Although the contents of the .NET Framework SDK are very rich, it is also very powerful with the functions he can achieve, but for some underlying operations, sometimes it is necessary to achieve the use of Windows API functions, and the key to achieving Screen Capture is to master C. # Method for calling an API function. I hope to pass this article, you can help you master the API programming in C #.

转载请注明原文地址:https://www.9cbs.com/read-87551.html

New Post(0)