无法使用DirectX捕获屏幕

时间:2013-02-13 10:33:50

标签: visual-c++ directx

AM试图在VS 2008中使用DirectX截屏。但我总是习惯于获得黑色图像。有人可以指出我,我的代码发生了什么错误..我的代码片段如下所示:

void CaptureScreen()
{
IDirect3DDevice9* pDirect3DDevice;

IDirect3DSurface9* pRenderTarget=NULL;
IDirect3DSurface9* pDestTarget=NULL;

D3DDISPLAYMODE d3ddisplaymode;

D3DPRESENT_PARAMETERS PresentParams;
memset(&PresentParams, 0, sizeof(D3DPRESENT_PARAMETERS));

PresentParams.Windowed = TRUE;
PresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD;

HWND h = ::FindWindow(0,"EIML");
IDirect3D9* direct=Direct3DCreate9(D3D9b_SDK_VERSION);
direct->CreateDevice(D3DADAPTER_DEFAULT,
            D3DDEVTYPE_HAL,
            h,
            D3DCREATE_SOFTWARE_VERTEXPROCESSING,
            &PresentParams,&pDirect3DDevice);

if (pDirect3DDevice == NULL)
return;

HRESULT hr = pDirect3DDevice->GetRenderTarget(0, &pRenderTarget);

hr = direct->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddisplaymode);

hr = pDirect3DDevice->CreateOffscreenPlainSurface(d3ddisplaymode.Width,d3ddisplaymode.Height,D3DFMT_A8R8G8B8,
D3DPOOL_SYSTEMMEM,
&pDestTarget,
NULL);

pDirect3DDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &pDestTarget ) ;

hr = pDirect3DDevice->GetRenderTargetData(pRenderTarget,pDestTarget);

hr = D3DXSaveSurfaceToFile("C:\\Image1.bmp",
D3DXIFF_BMP,
pDestTarget,
NULL,
NULL);

pRenderTarget->Release();
pDestTarget->Release();

 }

谢谢..

1 个答案:

答案 0 :(得分:2)

以下是我截取屏幕截图的方式:

IDirect3DSurface9 *offscreenSurface;
d3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &offscreenSurface);
D3DXSaveSurfaceToFile( filename, D3DXIFF_BMP, offscreenSurface, 0, 0 )