0

我试图在 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();

 }

谢谢..

4

1 回答 1

2

这是我截取屏幕截图的方式:

IDirect3DSurface9 *offscreenSurface;
d3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &offscreenSurface);
D3DXSaveSurfaceToFile( filename, D3DXIFF_BMP, offscreenSurface, 0, 0 )
于 2013-02-14T19:21:51.247 回答