c++ - 仅从指定窗口捕获像素数据
问题描述
我希望下面的代码只截取指定窗口的屏幕截图,因为这样 BitBlt 更快。下面的代码截取由窗口名称指定的窗口的屏幕截图,将像素数据加载到缓冲区中,然后在屏幕上重新绘制图片,以证明复制有效。
我想截取 Google Chrome 等浏览器窗口的屏幕截图,但它似乎不起作用。它在我的屏幕上绘制了一个具有正确窗口尺寸的黑色矩形。
似乎一直在使用 Minecraft 的窗口。
还与 Tor 浏览器一起工作。
我注意到在查询窗口信息后,我的世界的窗口没有child-windows,但是当它不能与其他窗口一起工作时,它们都有多个子窗口。
#include<iostream>
#include<Windows.h>
#include<vector>
using namespace std;
int main() {
HWND wnd = FindWindow(NULL, "Minecraft 1.15.1");//Name of window to be screenshoted
if (!wnd) {
std::cout << "e1\n";
std::cin.get();
return 0;
}
WINDOWINFO wi = { 0 };
wi.cbSize = sizeof(WINDOWINFO);
GetWindowInfo(wnd, &wi);
RECT rect;
GetWindowRect(wnd, &rect);
int width = wi.rcClient.right - wi.rcClient.left;
int height = wi.rcClient.bottom - wi.rcClient.top;
cout << width << height;
/////Fetch window info^^^^^^^^
BYTE* ScreenData = new BYTE[4 * width*height];
// copy screen to bitmap
HDC hScreen = GetWindowDC(wnd);
HDC hDC = CreateCompatibleDC(hScreen);
HBITMAP hBitmap = CreateCompatibleBitmap(hScreen, width, height);
HGDIOBJ old_obj = SelectObject(hDC, hBitmap);
BitBlt(hDC, 0, 0, width, height, hScreen, 0, 0, SRCCOPY);
SelectObject(hDC, old_obj);
BITMAPINFO bmi = { 0 };
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
std::cout << GetDIBits(hDC, hBitmap, 0, 0, NULL, &bmi, DIB_RGB_COLORS)<<endl;
//std::cout << bmi.bmiHeader.biHeight<< " "<< bmi.bmiHeader.biWidth<<endl;
BYTE*buffer = new BYTE[4* bmi.bmiHeader.biHeight*bmi.bmiHeader.biWidth];
bmi.bmiHeader.biHeight *= -1;
std::cout<<GetDIBits(hDC, hBitmap, 0, bmi.bmiHeader.biHeight, buffer, &bmi, DIB_RGB_COLORS)<<endl;//DIB_PAL_COLORS
/////Load window pixels into a buffer^^^^^^^^
POINT p;
int r = 0;
int g = 0;
int b = 0;
int x = 0;
int y = 0;
HDC sc = GetDC(NULL);
COLORREF color;
while (true) {
if ((y*-1) == bmi.bmiHeader.biHeight+1 && x == bmi.bmiHeader.biWidth-1) { break; }
r = (int)buffer[4 * ((y*bmi.bmiHeader.biWidth) + x)+2];
g = (int)buffer[4 * ((y*bmi.bmiHeader.biWidth) + x)+1];
b = (int)buffer[4 * ((y*bmi.bmiHeader.biWidth) + x) ];
color = RGB(r, g, b);
SetPixel(sc, x, y, color);
x++;
if (x == bmi.bmiHeader.biWidth) {
y++;
x = 0;
}
}
/////Prove that the copying was successful and buffer is full^^^^^^^^
Sleep(5000);
std::cout << "fin\n";
std::cin.get();
return 0;
}
解决方案
我认为问题在于你做事的顺序。
在将位图放到剪贴板上之前,您应该从内存设备上下文中选择它。
一旦您将位图放在剪贴板上,您就不再拥有它,因此您不应该尝试删除它。
最好的方法可能是将您的部分移到// clean up
该部分之前// save bitmap to clipboard
并删除您的DelectObject(hBitmap)
陈述。所以你的代码的结尾可能应该是:
BitBlt(hDC, 0, 0, width, height, hScreen, 0, 0, SRCCOPY);
// clean up
SelectObject(hDC, old_obj); // selects hBitmap out of hDC
DeleteDC(hDC);
ReleaseDC(NULL, hScreen);
// save bitmap to clipboard
OpenClipboard(NULL);
EmptyClipboard();
SetClipboardData(CF_BITMAP, hBitmap); // clipboard now owns the bitmap
CloseClipboard();
如果在这些更改之后仍然有问题,我会检查 SetClipboardData 调用的返回值。如果失败,GetLastError 可能会提供线索。
推荐阅读
- asp.net-core - 重新启动 IIS 网站后,Blazor 服务器端应用程序不会重定向到登录页面
- javascript - XPages 在按钮单击时创建一个新文件夹
- c++ - 如何修复错误“表达式必须具有指向对象类型”(多文件项目)?
- java - Spring JPA 数据 - 保存方法不会更新
- nginx - nginx 代理通行证:斜杠,vars 不起作用
- r - 更改 plotly-R 中的图例
- python - “属性错误:列表对象没有较低的属性”是什么意思?Sklearn 工具包(Countvectorizer)
- javascript - Array.forEach 函数覆盖 itsel
- java - 按钮单击时的 Android Studio 推送通知
- assembly - 如何在 GPR 的特定位中设置进位标志而不进行移位/旋转?