Use C # To create a screen capture program (3)
3. code writing:
First, add the external declaration of the BitBlt function described above to the code file.
Add the onClick event processing function of the button, which is the main part of screen capture. The function is as follows:
Private void button#click (object sender, System. EventArgs e)
{
Graphics g1 = this. CreateGraphics (); // obtain the form image object
Image MyImage = new Bitmap (this. ClientRectangle. Width, this. ClientRectangle. Height, g1 );
Graphics g2 = Graphics. FromImage (MyImage); // create a bitmap image object
IntPtr dc1 = g1.GetHdc (); // obtain the context device of the form
IntPtr dc2 = g2.GetHdc (); // context device for obtaining bitmap files
BitBlt (dc2, 0, 0, this. ClientRectangle. Width, this. ClientRectangle. Height, dc1, 0, 0, 13369376); // write in-place Graph
G1.ReleaseHdc (dc1); // release the context device of the form
G2.ReleaseHdc (dc2); // The context device that releases the bitmap file.
MyImage. Save (@ "c: Captured.jpg", ImageFormat. Jpeg); // Save as a jpeg file
MessageBox. Show ("saving the image ends! ");
}
4. So far, the program has been completed. Press Ctrl + F5 to try the following results:
The image has been saved. Check the screen capture result (for example )!
However, this program only captures the customer zone of the program itself, so the function is limited. Of course, you can also try a program to capture any position on the screen! You only need to change the width and height of the source image, and the width and height can be selected by the user. In this way, a self-made screen capture program is released.
From the above example, it is not difficult to find that it is very easy to use C # programming to implement some basic functions. It is really a good tool for efficient development. Therefore, I hope that more people will join the C # field and develop more practical and complete software.