截屏 C++ directx 得到黑色图像
我正在尝试使用 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;
}
I'm trying to take a screen capture with directx. I tried code from several pages that explains how but always get a black screen shot. If someone can help me I will be grateful.
my code is:
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试添加一些错误处理,也许您会知道它失败的原因。
Try adding some error handling and perhaps you will get to know why it fails.
我认为代码显示了黑色屏幕截图,因为您正在尝试保存前端缓冲区,而在您调用 D3DXSaveSurfaceToFile() 时该缓冲区是空的。您应该首先调用 IDirect3dDevice9::Present(),或者执行以下操作:
不会提供您在屏幕上看到的内容(至少根据我的经验!),只会提供窗口客户区的内容哪个它被创建了。
I think that the code shows a black screenshot because you are trying to save the front buffer, which at the time you call D3DXSaveSurfaceToFile() is empty. You should either call IDirect3dDevice9::Present() first, or do the following:
Direct3D will not give you the contents of what you see on the screen (at least to my experience!), only the contents of the client area of the window for which it was created.