我问how to take the screen shot in DirectX并且“使用GetFrontBufferData()”得到了回答。但是,“此功能在设计上非常慢,不应在任何性能关键路径中使用”在MSDN中有所描述。
当我向其他人询问测量时,“将后缓冲器的数据传输到系统存储器”得到了回答。但是,我无法找到具体的方法。
这是一个失败的例子。请让我知道正确的代码。
#include <stdio.h>
#include <Windows.h>
#include <d3d9.h>
#include <d3dx9tex.h>
void main()
{
CoInitialize(NULL);
HWND hwnd = GetDesktopWindow();
LPDIRECT3D9 d3d9;
D3DDISPLAYMODE ddm;
d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
d3d9->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&ddm);
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp,sizeof(D3DPRESENT_PARAMETERS));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferWidth = ddm.Width;
d3dpp.BackBufferHeight = ddm.Height;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.MultiSampleQuality = 0;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.hDeviceWindow = hwnd;
d3dpp.Flags = D3DPRESENTFLAG_VIDEO;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
LPDIRECT3DDEVICE9 dev;
d3d9->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING ,&d3dpp, &dev);
/* Deprecation
IDirect3DSurface9* surface = NULL;
dev->CreateOffscreenPlainSurface(ddm.Width, ddm.Height, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &surface, NULL);
dev->GetFrontBufferData(0, surface);
D3DXSaveSurfaceToFile(L"D:\\test.bmp", D3DXIFF_BMP, surface, NULL, NULL);
*/
// Transfer the data of a back buffer to the system memory
IDirect3DSurface9* surface = NULL;
dev->CreateOffscreenPlainSurface(ddm.Width, ddm.Height, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &surface, NULL);
LPDIRECT3DSURFACE9 back = NULL;
dev->SetRenderTarget(0, surface);
dev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &back);
dev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,127), 1.0, 0 );
dev->BeginScene();
dev->StretchRect(surface, NULL, back, NULL, D3DTEXF_NONE);
dev->EndScene();
dev->Present(0,0,0,0);
D3DXSaveSurfaceToFile(L"D:\\test.bmp", D3DXIFF_BMP, back, NULL, NULL);
if(back) back->Release();
dev->Release();
d3d9->Release();
CoUninitialize();
}
答案 0 :(得分:2)
您无法从后台缓冲区获取数据,函数GetBackBuffer只检索后台缓冲区的指针,而不是其中的数据。
这意味着您在桌面上看到的内容存在于前台缓冲区中。