windows directx9截屏保存bmp文件

2021-05-06 15:27

阅读:365

标签:ima   ges   create   plain   screen   .com   swa   pool   保存   

/*
 * windows directx9截屏保存bmp文件
 */
#include   
#include   
#pragma comment(lib, "D3D9.lib")  
#pragma comment(lib, "D3dx9.lib")

#ifndef SAFE_RELEASE
#define SAFE_RELEASE(p) if(p){p->Release();p = NULL;}
#endif

int 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截屏保存bmp文件

标签:ima   ges   create   plain   screen   .com   swa   pool   保存   

原文地址:http://www.cnblogs.com/hatsusakana/p/7660320.html


评论


亲,登录后才可以留言!