windows directx9截屏保存bmp文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了windows directx9截屏保存bmp文件相关的知识,希望对你有一定的参考价值。

/*
 * windows directx9截屏保存bmp文件
 */
#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;
}

directx9_bmp.cpp

以上是关于windows directx9截屏保存bmp文件的主要内容,如果未能解决你的问题,请参考以下文章

windows gdi截图保存bmp

directx截屏保存bmp文件

windows gdi截屏使用libjpeg保存jpg文件

vc 截取屏幕

截屏为什么都说应该保存为 PNG 格式

如何从视频截图