我正在尝试在Windows上捕获屏幕的方法,从而决定最快的方法。最常见的是GDI方式。而且性能也不错。根据系统负载和静态/非静态屏幕内容,屏幕捕获率范围为27-47 fps(Windows 7,Intel i5 @2.6 GHz,8 GB RAM)。
现在,使用DirectX前端缓冲区方法(使用GetFrontBufferData()API),性能可比,但速度较慢(我无法达到48 fps)。
我浏览了这篇文章:Fastest method of screen capturing并尝试了使用getRenderTarget()和getRenderTargetData()在接受的答案中建议的方式,但正如评论中所建议的那样,我得到的只是一个黑色图像。这是我的完整代码,包括设备的初始配置:
IDirect3DSurface9* pRenderTarget=NULL;
IDirect3DSurface9* pDestTarget=NULL;
// sanity checks.
if (g_pd3dDevice == NULL){
return;
}
HRESULT hr;
// get the render target surface.
hr = g_pd3dDevice->GetRenderTarget(0, &pRenderTarget);
// get the current adapter display mode.
//hr = pDirect3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddisplaymode);
// create a destination surface.
hr = g_pd3dDevice->CreateOffscreenPlainSurface(1600, 900, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &pDestTarget, NULL);
//copy the render target to the destination surface.
hr = g_pd3dDevice->GetRenderTargetData(pRenderTarget, pDestTarget);
D3DLOCKED_RECT lockedRect;
hr =pDestTarget->LockRect(&lockedRect,NULL, D3DLOCK_NO_DIRTY_UPDATE|D3DLOCK_NOSYSLOCK|D3DLOCK_READONLY);
for( int i=0 ; i < 900 ; i++)
{
for(int j=0; j < 1600; j++){
memcpy( (BYTE*) data + i * 1600 * 3 + j * 3, (BYTE*) lockedRect.pBits + i* lockedRect.Pitch + j * 4, 3);
}
}
pDestTarget->UnlockRect();
// clean up.
pRenderTarget->Release();
pDestTarget->Release();
对于设备初始化部分:
g_pDirect3D = Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS PresentParams;
memset(&PresentParams, 0, sizeof(D3DPRESENT_PARAMETERS));
PresentParams.Windowed = TRUE;
PresentParams.SwapEffect =D3DSWAPEFFECT_DISCARD;
g_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL ,GetDesktopWindow(),D3DCREATE_SOFTWARE_VERTEXPROCESSING, &PresentParams,&g_pd3dDevice);
请指出上述代码中的错误。欢迎任何建议。我也尝试了GetBackBuffer()API,但我无法掌握图像数据。
修改
我的用例是捕获桌面屏幕,这似乎只能通过directX使用GetFrontBufferData()来完成。 GetBackBuffer()/ GetRenderTargetData()不是调用此案例的正确API。参考:The desktop capture with DirectX does not work和How to get the screenshot of the desktop from back buffer with DirectX。
但是,欢迎任何有关更快捕获桌面屏幕或优化GDI方式的建议。
答案 0 :(得分:2)
在拍摄截图之前你有没有渲染过什么?
所以如果你没有渲染任何东西,你肯定会得到一张空白的图像。