发现一个up主使用的工具 里面的图片采集功能挺有意思 作为一名热爱coding的技术小白,我立刻就萌生了自己也动手开发一个类似工具的想法。
[C#] 纯文本查看 复制代码 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++;
}
}
下载 ---> https://wwz.lanzoue.com/ivvi31zmobze 解压密码为 52pj
|