C # capture screen images using windowsapi

Source: Internet
Author: User

Add reference

Using system. Data;
Using system. Drawing. imaging;

Call the API and save the image

[System. runtime. interopservices. dllimportattribute ("gdi32.dll")]

Private Static extern bool bitblt (

Intptr hdcdest, // The destination DC handle

Int nxdest, // X coordinate in the upper left corner of the Target Image

Int nydest, // y coordinate in the upper left corner of the Target Image

Int nwidth, // The width of the rectangle of the Target Image

Int nheight, // The height of the rectangle of the Target Image

Intptr hdcsrc, // source DC handle

Int nxsrc, // X coordinate in the upper left corner of the source Image

Int nysrc, // X coordinate in the upper left corner of the source Image

System. int32 dwdrop // grating operation code

);

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! ");

}

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.