Skip to main content

连续截图

void UpdateScreen(object state, EventArgs e) {
lock (lockObject) {
if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text)) {
btnStartCapture.Text = "开始截图";
timer.Tick -= new EventHandler(UpdateScreen);
BtnStartCapture_Flag = !BtnStartCapture_Flag;
MessageBox.Show("未选择截图方式", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}

if (isHandleMode){
RECT rect2;
GetWindowRect(hWnd, out rect2);
int width = rect2.Right - rect2.Left;
int height = rect2.Bottom - rect2.Top;

IntPtr hdcWindow = GetWindowDC(hWnd);
IntPtr hdcCompatible = CreateCompatibleDC(hdcWindow);
IntPtr hBitmap = CreateCompatibleBitmap(hdcWindow, width, height);

SelectObject(hdcCompatible, hBitmap);
BitBlt(hdcCompatible, 0, 0, width, height, hdcWindow, 0, 0, 0x00CC0020); // CAPTUREBLT flag to capture layered windows

img = Image.FromHbitmap(hBitmap);

if (picBox.InvokeRequired) {
picBox.Invoke((MethodInvoker)delegate {
picBox.Image = img;
});
} else {
picBox.Image = img;
}

DeleteObject(hBitmap);
DeleteDC(hdcCompatible);
DeleteDC(hdcWindow);
} else {
using (Graphics g = Graphics.FromImage(img)) {
g.CopyFromScreen(Point.Empty, Point.Empty, img.Size);
}

if (picBox.InvokeRequired) {
picBox.Invoke((MethodInvoker)delegate {
picBox.Image = img;
});
} else {
picBox.Image = img;
}
}

filePath = string.Format("images/screenshot_{0}.{1}", captureCount, format);
img.Save(filePath);

if (captureCount == int.Parse(textBox4.Text)) {
btnStartCapture.Text = "开始截图";
timer.Tick -= new EventHandler(UpdateScreen);
BtnStartCapture_Flag = !BtnStartCapture_Flag;
MessageBox.Show("已完成", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
captureCount++;
}
}