码迷,mamicode.com
首页 > 其他好文 > 详细

directx截屏保存bmp文件

时间:2017-11-06 14:53:06      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:ack   size   define   swap   har   desktop   pool   mod   device   

#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;}
#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;
}

  

directx截屏保存bmp文件

标签:ack   size   define   swap   har   desktop   pool   mod   device   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!