Use C # To create a screen capture program

Source: Internet
Author: User

Author: Ma Jinhu

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: \ mydomain.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 ("-"

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.