/* * Windows DirectX9 screenshot save BMP File */#include <d3d9.h> #include <D3dx9tex.h> #pragma comment (lib, "D3D9.lib") # pragma comment (lib, "D3dx9.lib") #ifndef safe_release#define safe_release (P) if (p) {p->release ();p = NULL;} #endifint Main (int Argc,char *argv[]) {Lpdirect3ddevice9 g_pd3ddevice = NULL; Lpdirect3dsurface9 g_pd3dsurface = NULL; HWND Dthwnd = GetDesktopWindow (); Lpdirect3d9 G_pd3d = Direct3dcreate9 (d3d_sdk_version); if (G_pd3d = = NULL) return-1;d3dpresent_parameters d3dpp; ZeroMemory (&d3dpp,sizeof (D3DPP)); D3DPP. windowed = true; D3DPP. Flags = D3dpresentflag_lockable_backbuffer; D3DPP. SwapEffect = D3dswapeffect_discard; D3dpp.hdevicewindow = Dthwnd; HRESULT hr = G_pd3d->createdevice (D3dadapter_default, D3ddevtype_hal, Dthwnd, d3dcreate_hardware_vertexprocessing , &D3DPP, &g_pd3ddevice); if (hr! = D3D_OK | | g_pd3ddevice = = NULL) {return-1;} D3ddisplaymode DDM = {0}; hr = G_pd3ddevice->getdisplaymode (0, &DDM); if (hr! = D3D_OK) {safe_release (g_pd3d); return-1;} hr = G_pd3ddevice->createoffscreenplainsurface (DDM. Width, DDM. Height, D3dfmt_a8r8g8b8, D3dpool_systemmem, &g_pd3dsurface, NULL); if (hr! = D3D_OK) {safe_release (g_pd3ddevice); Safe_release (G_PD3D); return-1;} if (g_pd3ddevice && g_pd3dsurface) {hr = G_pd3ddevice->getfrontbufferdata (0, G_pd3dsurface); if (hr = = D3D_OK) {hr = D3dxsavesurfacetofile ("Directx9.bmp", d3dxiff_bmp, g_pd3dsurface, NULL, NULL); }}safe_release (G_pd3dsurface); Safe_release (g_pd3ddevice); Safe_release (G_PD3D); return 1;}
Directx9_bmp.cpp
Windows DirectX9 screenshot Save BMP file