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