Use C # To create a screen capture program (3)

Source: Internet
Author: User

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.

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.