我知道如何使用GDI捕获屏幕,但它很慢(它几乎没有捕获10 fps)
我已经读过DirectX提供最佳速度.但在我开始学习DirectX之前,我想测试一个样本,看看它是否真的那么快.
我发现这个问题提供了一个示例代码来执行此操作:
void dump_buffer()
{
IDirect3DSurface9* pRenderTarget=NULL;
IDirect3DSurface9* pDestTarget=NULL;
const char file[] = "Pickture.bmp";
// sanity checks.
if (Device == NULL)
return;
// get the render target surface.
HRESULT hr = Device->GetRenderTarget(0, &pRenderTarget);
// get the current adapter display mode.
//hr = pDirect3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddisplaymode);
// create a destination surface.
hr = Device->CreateOffscreenPlainSurface(DisplayMde.Width,
DisplayMde.Height,
DisplayMde.Format,
D3DPOOL_SYSTEMMEM,
&pDestTarget,
NULL);
//copy the render target to the destination surface.
hr = Device->GetRenderTargetData(pRenderTarget, pDestTarget);
//save its contents to a bitmap file.
hr …Run Code Online (Sandbox Code Playgroud) 我的程序正在截取其他应用程序窗口的屏幕截图,以自动执行其中的某些任务。这些窗口可能会隐藏在屏幕外或不时被其他窗口遮挡。
为了减少混乱,我从代码列表中删除了所有错误检查。我正在准备两种类型的屏幕截图
// Get size of the target window.
RECT clientRect;
GetClientRect(hwnd, &clientRect);
int width = clientRect.right - clientRect.left;
int height = clientRect.bottom - clientRect.top;
// Create a memory device context.
HDC windowDC = GetDC(hwnd);
HDC memoryDC = CreateCompatibleDC(windowDC);
// Create a bitmap for rendering.
BITMAPINFO bitmapInfo;
ZeroMemory(&bitmapInfo, sizeof(BITMAPINFO));
bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bitmapInfo.bmiHeader.biWidth = width;
bitmapInfo.bmiHeader.biHeight = -height;
bitmapInfo.bmiHeader.biPlanes = 1;
bitmapInfo.bmiHeader.biBitCount = 32;
bitmapInfo.bmiHeader.biCompression = BI_RGB;
bitmapInfo.bmiHeader.biSizeImage = width * height * 4;
RGBQUAD* buffer;
HBITMAP bitmap = CreateDIBSection(windowDC, …Run Code Online (Sandbox Code Playgroud) 我正在使用BitBltwinapi 函数截取给定窗口的屏幕截图,即使该窗口部分重叠。
我已经一切正常,除了在 Windows 10 上,对于某些窗口(如 Edge 浏览器),屏幕截图完全是黑色的。
许多其他问题都同意将此问题归因于在这些窗口上使用硬件加速图形上下文。
显然 GDI 库与那种图形上下文不兼容,因此必须使用不同的库。
我的问题特别是关于如何检测窗口是否使用与 GDI 库不兼容的图形上下文。
如果我能够检测到这一点,那么我可以选择使用哪个库来正确捕获屏幕截图(GDI、ActiveX 或其他)。
否则,我可以检测到的唯一方法是逐像素扫描屏幕截图以检查它是否完全黑色。然后使用不同的捕获方法,直到我在屏幕截图上获得一些内容。
但这听起来像是一个糟糕的解决方案。