简体   繁体   English

DirectX屏幕截图:为什么GetBackBuffer总是给我黑色图像?

[英]DirectX screen capture: Why GetBackBuffer always gives me a black image?

My problem is about capturing the full screen image using DirectX ,no matter what applications are running. 我的问题是无论运行什么应用程序,都使用DirectX捕获全屏图像。 I tried to capture the screen by using the GetBackBuffer function (because I've been warned that GetFrontBufferData is very slow ,as slow as GDI) 我尝试使用GetBackBuffer函数捕获屏幕(因为已经警告我GetFrontBufferData太慢了,就像GDI一样慢)

However, when I tried it, GetFrontBufferData works fine for capturing the screen, but GetBackBuffer always gives me a black image . 但是,当我尝试使用它时,GetFrontBufferData可以很好地捕获屏幕,但是GetBackBuffer总是给我黑色图像。

Some people told me that GetBackBuffer only gets what you draw on the screen. 有人告诉我,GetBackBuffer只获取您在屏幕上绘制的内容。 If you draw nothing ,you get nothing. 如果什么都不画,什么也得不到。 If you want to capture the screen image by using GetBackBuffer function ,that is impossible. 如果要使用GetBackBuffer函数捕获屏幕图像,那是不可能的。 Is that right ? 那正确吗 ?

My attempted code is: 我尝试的代码是:

LPDIRECT3D9 g_pD3D = NULL;
D3DDISPLAYMODE ddm;
D3DPRESENT_PARAMETERS d3dpp;
IDirect3DDevice9 * g_pd3dDevice;
IDirect3DSurface9 * pSurface;
ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));
ZeroMemory(&d3dpp, sizeof(d3dpp));
g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &ddm);
d3dpp.Windowed = TRUE;
d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
d3dpp.BackBufferFormat = ddm.Format;
d3dpp.BackBufferHeight = ddm.Height;
d3dpp.BackBufferWidth = ddm.Width;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = GetDesktopWindow();
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
HRESULT hr = g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, GetDesktopWindow(), D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice);
hr = g_pd3dDevice->CreateOffscreenPlainSurface(ddm.Width, ddm.Height, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &pSurface, NULL);
//hr = g_pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &pSurface);//Getbackbuffer gives me a black image
hr = g_pd3dDevice->GetFrontBufferData(0, pSurface);//getfrontbufferdata works well
hr = D3DXSaveSurfaceToFile("d:/1.png", D3DXIFF_PNG, pSurface, NULL, NULL);
pSurface->UnlockRect();
pSurface->Release();
g_pd3dDevice->Release();
g_pD3D->Release();

The people that told you that GetBackbuffer only gets what you've drawn are telling you the truth. 告诉您GetBackbuffer仅获取您绘制的内容的人正在告诉您真相。 Each application that uses Direct3D has its own back buffers that contain only what that application draws into them. 每个使用Direct3D的应用程序都有自己的后台缓冲区,这些缓冲区仅包含该应用程序绘制到其中的内容。 Your application has its own back buffer that contains only what your application draws into it. 您的应用程序有自己的后台缓冲区,该缓冲区仅包含您的应用程序提取到的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM