我正在尝试使用directx进行屏幕截图。我尝试了几页的代码,解释了如何但总是得到黑屏幕。如果有人能帮助我,我将不胜感激。
我的代码是:
int _tmain(int argc, _TCHAR* argv[])
{
IDirect3DDevice9* g_pd3dDevice;
//SaveScreenShot("test.bmp",D3DXIFF_BMP);
IDirect3DSurface9* pSurface=NULL;
//IDirect3DSurface9* pSource=NULL;
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = FALSE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferWidth = 1280;
d3dpp.BackBufferHeight = 1024;
d3dpp.hDeviceWindow = GetDesktopWindow();
d3dpp.FullScreen_RefreshRateInHz=0;
d3dpp.MultiSampleQuality=0;
IDirect3D9* direct=Direct3DCreate9(D3D9b_SDK_VERSION);
direct->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
GetDesktopWindow(),
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,&g_pd3dDevice);
//g_pd3dDevice->GetRenderTarget(0,&pSource);
g_pd3dDevice->CreateOffscreenPlainSurface(1280, 1024,
D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &pSurface, NULL);
g_pd3dDevice->GetFrontBufferData(0, pSurface);
/*g_pd3dDevice->GetRenderTargetData(pSource,pSurface);
g_pd3dDevice->GetRenderTargetData(pSource,pSurface);*/
D3DXSaveSurfaceToFile("Desktop.jpg",D3DXIFF_JPG,pSurface,NULL,NULL);
pSurface->Release();
//pSource->Release();
return 0;
}
答案 0 :(得分:5)
尝试添加一些错误处理,也许你会知道它失败的原因。
答案 1 :(得分:1)
我认为代码显示黑色屏幕截图,因为您正在尝试保存前缓冲区,当前调用D3DXSaveSurfaceToFile()时为空。您应该首先调用IDirect3dDevice9 :: Present(),或者执行以下操作:
Direct3D不会提供你在屏幕上看到的内容(至少根据我的经验!),只提供创建它的窗口客户区的内容。