Windows 4K截屏直接存入缓冲区
4K screen capturing in Windows and directly save into a buffer
我知道网络上有很多帖子在 Windows 中使用 GDI 或 DirectX 方法进行屏幕捕获。但是,我发现的所有内容都将捕获的图像保存到位图中,而我想将其保存到缓冲区中。这是我以 GDi 方式执行此操作的代码:
HWND hwind = GetDesktopWindow();
HDC hdc = GetDC(hwind);
uint32_t resx = GetSystemMetrics(SM_CXSCREEN);
uint32_t resy = GetSystemMetrics(SM_CYSCREEN);
uint32_t BitsPerPixel = GetDeviceCaps(hdc, BITSPIXEL);
HDC hdc2 = CreateCompatibleDC(hdc);
BITMAPINFO info;
info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
info.bmiHeader.biWidth = resx;
info.bmiHeader.biHeight = resy;
info.bmiHeader.biPlanes = 1;
info.bmiHeader.biBitCount = BitsPerPixel;
info.bmiHeader.biCompression = BI_RGB;
void *data;
static HBITMAP hbitmap = CreateDIBSection(hdc2, &info, DIB_RGB_COLORS,
(void**)&data, 0, 0);
SelectObject(hdc2, hbitmap);
BitBlt(hdc2, 0, 0, resx, resy, hdc, 0, 0, SRCCOPY);
uint8_t *ptr = new uint8_t[4 * resx * resy];
uint32_t lineSizeSrc = 4 * resx; // not always correct
uint32_t linesizeDst = 4 * resx;
for (uint32_t y = 0; y < resy; y++)
memcpy(ptr + y * lineSizeDst,
(uint8_t*) data + y * lineSizeSrc,
lineSizeDst);
DeleteObject(hbitmap);
ReleaseDC(hwind, hdc);
if (hdc2) {
DeleteDC(hdc2);
}
首先,据我所知,此代码中 lineSizeSrc
的值并不总是正确的,因为根据屏幕分辨率的不同,可能会在 data
的每一行添加一些零。谁能解释一下何时添加零以及如何获得 lineSizeSrc
的正确值?
其次,是否可以不考虑显示器的分辨率而获得4K分辨率的捕获图像,例如强制显卡以4K分辨率输出?
大多数现代显示器都支持 32 位颜色,这相对简单,因为它不需要调色板。 C++
中的示例:
void capture(char* &buffer)
{
HWND hwnd = GetDesktopWindow();
HDC hdc = GetDC(hwnd);
int w = GetSystemMetrics(SM_CXSCREEN);
int h = GetSystemMetrics(SM_CYSCREEN);
int BitsPerPixel = GetDeviceCaps(hdc, BITSPIXEL);
if (BitsPerPixel = 32)
{
HDC memdc = CreateCompatibleDC(hdc);
HBITMAP bmp = CreateCompatibleBitmap(hdc, w, h);
HGDIOBJ oldbitmap = SelectObject(memdc, bmp);
BitBlt(memdc, 0, 0, w, h, hdc, 0, 0, CAPTUREBLT | SRCCOPY);
SelectObject(memdc, oldbitmap);
DWORD bitsize = w * h * 4;
char *bits = new char[bitsize];
DWORD szInfoHdr = sizeof(BITMAPINFOHEADER);
BITMAPINFOHEADER bmpInfoHeader =
{ szInfoHdr, w, h, 1, (WORD)BitsPerPixel, BI_RGB, 0, 0, 0, 0, 0 };
GetDIBits(hdc, bmp, 0, h, bits, (BITMAPINFO*)&bmpInfoHeader, DIB_RGB_COLORS);
buffer = new char[bitsize + szInfoHdr];
memcpy(buffer, &bmpInfoHeader, szInfoHdr);
memcpy(buffer + szInfoHdr, bits, bitsize);
delete[]bits;
DeleteObject(bmp);
DeleteObject(memdc);
}
ReleaseDC(hwnd, hdc);
}
您可以通过函数传递 buffer
。可以使用以下代码进行测试:
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
char *buffer = 0;
//capture the screen and save to buffer
capture(buffer);
if (buffer)
{
//paint the buffer for testing:
BITMAPINFO* bmpinfo = (BITMAPINFO*)buffer;
if (bmpinfo->bmiHeader.biBitCount == 32)
{
int w = bmpinfo->bmiHeader.biWidth;
int h = bmpinfo->bmiHeader.biHeight;
char *bits = buffer + sizeof(BITMAPINFOHEADER);
HBITMAP hbitmap = CreateDIBitmap(hdc,
&bmpinfo->bmiHeader, CBM_INIT, bits, bmpinfo, DIB_RGB_COLORS);
HDC memdc = CreateCompatibleDC(hdc);
SelectObject(memdc, hbitmap);
BitBlt(hdc, 0, 0, w, h, memdc, 0, 0, SRCCOPY);
}
delete[]buffer;
}
EndPaint(hWnd, &ps);
}
但请注意,GetSystemMetrics(SM_CXSCREEN)
returns 仅主显示器的宽度。
您可能希望 SM_CXVIRTUALSCREEN
和 SM_CYVIRTUALSCREEN
获得多显示器的 width/height。使用 SM_(X/Y)VIRTUALSCREEN
获取左上角。
First, as far as I know, the value of lineSizeSrc in this code is not always correct since depending on the screen resolution, some zeros may be added to each line of data. Can anyone please explain when the zeros are added and how to get the correct value for lineSizeSrc
?
位图格式要求每行的起始地址是 4 字节的倍数。通常,这是可行的,因为常见的图像宽度是 4 的倍数,或者因为单个像素的大小是 32 位(即 4 个字节)。
但是,如果您要表示的图像具有不寻常的宽度(例如,31 像素宽)并且每个像素使用 24 位(3 字节)之类的东西,那么您需要在每行的末尾进行填充,以便下一行从 4 的倍数开始。
一个常见的方法是将 "stride":
lineSizeSrc = (resx * BitsPerPixel + 31) / 8;
resx * BitsPerPixel
告诉我们表示该行所需的位数。除以 8 将位转换为字节——某种程度上。整数除法会截断任何余数。通过首先添加 31,我们确保截断为我们提供等于或大于我们需要的位数的 32 位(4 字节)的最小倍数。所以lineSizeSrc
是每行需要的字节数。
在计算您需要多少字节时,您应该使用 lineSizeSrc
而不是 resx
。
Second, is it possible to get the captured image in 4K resolution regardless of the resolution of the monitor, for instance by forcing the graphics card to output in 4K resolution?
没有一种简单的、适用于所有情况的方法。最好的办法可能是要求程序渲染为 4K 的 window,即使显卡不在该模式下也是如此。有些程序会支持这一点,但其他程序现在可能会支持。查看 WM_PRINT
和 WM_PRINTCLIENT
消息的文档。
我知道网络上有很多帖子在 Windows 中使用 GDI 或 DirectX 方法进行屏幕捕获。但是,我发现的所有内容都将捕获的图像保存到位图中,而我想将其保存到缓冲区中。这是我以 GDi 方式执行此操作的代码:
HWND hwind = GetDesktopWindow();
HDC hdc = GetDC(hwind);
uint32_t resx = GetSystemMetrics(SM_CXSCREEN);
uint32_t resy = GetSystemMetrics(SM_CYSCREEN);
uint32_t BitsPerPixel = GetDeviceCaps(hdc, BITSPIXEL);
HDC hdc2 = CreateCompatibleDC(hdc);
BITMAPINFO info;
info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
info.bmiHeader.biWidth = resx;
info.bmiHeader.biHeight = resy;
info.bmiHeader.biPlanes = 1;
info.bmiHeader.biBitCount = BitsPerPixel;
info.bmiHeader.biCompression = BI_RGB;
void *data;
static HBITMAP hbitmap = CreateDIBSection(hdc2, &info, DIB_RGB_COLORS,
(void**)&data, 0, 0);
SelectObject(hdc2, hbitmap);
BitBlt(hdc2, 0, 0, resx, resy, hdc, 0, 0, SRCCOPY);
uint8_t *ptr = new uint8_t[4 * resx * resy];
uint32_t lineSizeSrc = 4 * resx; // not always correct
uint32_t linesizeDst = 4 * resx;
for (uint32_t y = 0; y < resy; y++)
memcpy(ptr + y * lineSizeDst,
(uint8_t*) data + y * lineSizeSrc,
lineSizeDst);
DeleteObject(hbitmap);
ReleaseDC(hwind, hdc);
if (hdc2) {
DeleteDC(hdc2);
}
首先,据我所知,此代码中 lineSizeSrc
的值并不总是正确的,因为根据屏幕分辨率的不同,可能会在 data
的每一行添加一些零。谁能解释一下何时添加零以及如何获得 lineSizeSrc
的正确值?
其次,是否可以不考虑显示器的分辨率而获得4K分辨率的捕获图像,例如强制显卡以4K分辨率输出?
大多数现代显示器都支持 32 位颜色,这相对简单,因为它不需要调色板。 C++
中的示例:
void capture(char* &buffer)
{
HWND hwnd = GetDesktopWindow();
HDC hdc = GetDC(hwnd);
int w = GetSystemMetrics(SM_CXSCREEN);
int h = GetSystemMetrics(SM_CYSCREEN);
int BitsPerPixel = GetDeviceCaps(hdc, BITSPIXEL);
if (BitsPerPixel = 32)
{
HDC memdc = CreateCompatibleDC(hdc);
HBITMAP bmp = CreateCompatibleBitmap(hdc, w, h);
HGDIOBJ oldbitmap = SelectObject(memdc, bmp);
BitBlt(memdc, 0, 0, w, h, hdc, 0, 0, CAPTUREBLT | SRCCOPY);
SelectObject(memdc, oldbitmap);
DWORD bitsize = w * h * 4;
char *bits = new char[bitsize];
DWORD szInfoHdr = sizeof(BITMAPINFOHEADER);
BITMAPINFOHEADER bmpInfoHeader =
{ szInfoHdr, w, h, 1, (WORD)BitsPerPixel, BI_RGB, 0, 0, 0, 0, 0 };
GetDIBits(hdc, bmp, 0, h, bits, (BITMAPINFO*)&bmpInfoHeader, DIB_RGB_COLORS);
buffer = new char[bitsize + szInfoHdr];
memcpy(buffer, &bmpInfoHeader, szInfoHdr);
memcpy(buffer + szInfoHdr, bits, bitsize);
delete[]bits;
DeleteObject(bmp);
DeleteObject(memdc);
}
ReleaseDC(hwnd, hdc);
}
您可以通过函数传递 buffer
。可以使用以下代码进行测试:
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
char *buffer = 0;
//capture the screen and save to buffer
capture(buffer);
if (buffer)
{
//paint the buffer for testing:
BITMAPINFO* bmpinfo = (BITMAPINFO*)buffer;
if (bmpinfo->bmiHeader.biBitCount == 32)
{
int w = bmpinfo->bmiHeader.biWidth;
int h = bmpinfo->bmiHeader.biHeight;
char *bits = buffer + sizeof(BITMAPINFOHEADER);
HBITMAP hbitmap = CreateDIBitmap(hdc,
&bmpinfo->bmiHeader, CBM_INIT, bits, bmpinfo, DIB_RGB_COLORS);
HDC memdc = CreateCompatibleDC(hdc);
SelectObject(memdc, hbitmap);
BitBlt(hdc, 0, 0, w, h, memdc, 0, 0, SRCCOPY);
}
delete[]buffer;
}
EndPaint(hWnd, &ps);
}
但请注意,GetSystemMetrics(SM_CXSCREEN)
returns 仅主显示器的宽度。
您可能希望 SM_CXVIRTUALSCREEN
和 SM_CYVIRTUALSCREEN
获得多显示器的 width/height。使用 SM_(X/Y)VIRTUALSCREEN
获取左上角。
First, as far as I know, the value of lineSizeSrc in this code is not always correct since depending on the screen resolution, some zeros may be added to each line of data. Can anyone please explain when the zeros are added and how to get the correct value for
lineSizeSrc
?
位图格式要求每行的起始地址是 4 字节的倍数。通常,这是可行的,因为常见的图像宽度是 4 的倍数,或者因为单个像素的大小是 32 位(即 4 个字节)。
但是,如果您要表示的图像具有不寻常的宽度(例如,31 像素宽)并且每个像素使用 24 位(3 字节)之类的东西,那么您需要在每行的末尾进行填充,以便下一行从 4 的倍数开始。
一个常见的方法是将 "stride":
lineSizeSrc = (resx * BitsPerPixel + 31) / 8;
resx * BitsPerPixel
告诉我们表示该行所需的位数。除以 8 将位转换为字节——某种程度上。整数除法会截断任何余数。通过首先添加 31,我们确保截断为我们提供等于或大于我们需要的位数的 32 位(4 字节)的最小倍数。所以lineSizeSrc
是每行需要的字节数。
在计算您需要多少字节时,您应该使用 lineSizeSrc
而不是 resx
。
Second, is it possible to get the captured image in 4K resolution regardless of the resolution of the monitor, for instance by forcing the graphics card to output in 4K resolution?
没有一种简单的、适用于所有情况的方法。最好的办法可能是要求程序渲染为 4K 的 window,即使显卡不在该模式下也是如此。有些程序会支持这一点,但其他程序现在可能会支持。查看 WM_PRINT
和 WM_PRINTCLIENT
消息的文档。