我通过网络进行了一些研究,并找到了一些有用的代码。我改变了一下,试图捕获整个屏幕并生成一个我可以通过udp数据包发送的缓冲区:
#include <iostream>
#include <Windows.h>
#include <fstream>
void CapruteScreenAndSaveToFile()
{
uint16_t BitsPerPixel = 24;
uint32_t Width = GetSystemMetrics(SM_CXSCREEN);
uint32_t Height = GetSystemMetrics(SM_CYSCREEN);
// Create Header
BITMAPFILEHEADER Header;
memset(&Header, 0, sizeof(Header));
Header.bfType = 0x4D42;
Header.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// Create Info
BITMAPINFO Info;
memset(&Info, 0, sizeof(Info));
Info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
Info.bmiHeader.biWidth = Width;
Info.bmiHeader.biHeight = Height;
Info.bmiHeader.biPlanes = 1;
Info.bmiHeader.biBitCount = BitsPerPixel;
Info.bmiHeader.biCompression = BI_RGB;
Info.bmiHeader.biSizeImage = Width * Height * (BitsPerPixel > 24 ? 4 : 3);
// Capture screen and save to Pixels
char* Pixels = NULL;
HDC MemDC = CreateCompatibleDC(0);//Context);
HBITMAP Section = CreateDIBSection(MemDC, &Info, DIB_RGB_COLORS, (void**)&Pixels, 0, 0);
DeleteObject(SelectObject(MemDC, Section));
BitBlt(MemDC, 0, 0, Width, Height, GetDC(0), 0, 0, SRCCOPY);
DeleteDC(MemDC);
// Concatenate everything
char * buffer = (char*)malloc(sizeof(Header) + sizeof(Info.bmiHeader) + (((BitsPerPixel * Width + 31) & ~31) / 8) * Height);
memcpy(buffer, (char*)&Header, sizeof(Header));
memcpy(buffer + sizeof(Header), (char*)&Info.bmiHeader, sizeof(Info.bmiHeader));
memcpy(buffer + sizeof(Header) + sizeof(Info.bmiHeader), Pixels, (((BitsPerPixel * Width + 31) & ~31) / 8) * Height);
// Save to file
std::fstream hFile("Foo.bmp", std::ios::out | std::ios::binary);
hFile.write(buffer, sizeof(Header) + sizeof(Info.bmiHeader) + (((BitsPerPixel * Width + 31) & ~31) / 8) * Height);
// Clean up
hFile.close();
DeleteObject(Section);
free(buffer);
}
int main()
{
CapruteScreenAndSaveToFile();
return 0;
}
但它似乎只捕获了我桌面的这一部分:
即使我使用CreateCompatibleDC(0)也是如此。
答案 0 :(得分:5)
如果计算机处于高DPI设置,并且应用程序不支持DPI,则系统将欺骗应用程序并提供错误的屏幕大小。
您会发现以下代码显示的宽度和高度小于实际屏幕尺寸:
uint32_t Width = GetSystemMetrics(SM_CXSCREEN);
uint32_t Height = GetSystemMetrics(SM_CYSCREEN);
std::cout << Width << " x " << Height << "\n";
解决方案是将DPI awareness添加到应用程序。
添加DPI兼容性:
在Visual Studio 2015中,转到项目属性 - &gt;清单工具,将DPI意识设置为&#34; Per Monitor High DPI Aware&#34;或者&#34;高DPI意识&#34;
如果您使用的是旧编译器......
1)创建一个文件&#34; myapp.manifest&#34;有了这个内容:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">True/PM</dpiAware>
</windowsSettings>
</application>
</assembly>
2)使用以下内容将* .rc文件添加到您的项目中:
1 24 "myapp.manifest"
3)重建项目