Example of c implementing winform screen capture and saving
- 2020-06-12 10:31:30
- OfStack
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool BitBlt(
IntPtr hdcDest, // The target DC The handle of
int nXDest,
int nYDest,
int nWidth,
int nHeight,
IntPtr hdcSrc, // The source DC The handle of
int nXSrc,
int nYSrc,
System.Int32 dwRop // The processing value of the grating
);
private void button1_Click(object sender, System.EventArgs e)
{
// Gets the current screen size
Rectangle rect = new Rectangle ( ) ;
rect = Screen.GetWorkingArea ( this ) ;
// create 1 An image based on the current screen
Graphics g1 = this.CreateGraphics ( ) ;
// Create bitmaps based on screen size
Image MyImage = new Bitmap ( rect.Width , rect.Height , g1 ) ;
Graphics g2 = Graphics.FromImage ( MyImage ) ;
// Get the screen DC
IntPtr dc1 = g1.GetHdc ( ) ;
// get Bitmap the DC
IntPtr dc2 = g2.GetHdc ( ) ;
// Call this API Function to achieve screen capture
BitBlt ( dc2 , 0 , 0 , rect.Width , rect.Height , dc1 , 0 , 0 , 13369376 ) ;
// Let go of the screen DC
g1.ReleaseHdc ( dc1 ) ;
// release Bitmap the DC
g2.ReleaseHdc ( dc2 ) ;
// In order to JPG File format to save
MyImage.Save ( @"c:/Capture.jpg" , ImageFormat.Jpeg );
MessageBox.Show ( " The current screen has been saved as C The plate capture.jpg File! " ) ;
}