// DxShowImage.h
#ifndef _DXSHOWIMAGE_H_ #define _DXSHOWIMAGE_H_ #include <d3d9.h> #define SAFE_RELEASE(p) / if (p)/ {/ p->Release();/ p = NULL;/ } #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_TEX1) struct CUSTOMVERTEX { float x,y,z; float u,v; }; class CDxShowImage { public: CDxShowImage(); ~CDxShowImage(); public: bool Init(HWND hWnd); void Uninit(); // 有完整拉伸 bool ShowImageByTexture(const char* szFileName); bool ShowImageByTexture(const char* pbSrcData,int iSrcDataSize); bool ShowImageByOffscreen(const char* szFileName,int w,int h); bool ShowImageByOffscreen(const char* pbSrcData,int iSrcDataSize,int h); // 无完整拉伸 bool ShowImageBySurface(const char* szFileName); bool ShowImageBySurface(const char* pbSrcData,int iSrcDataSize); bool ShowImageByBackBuffer(const char* szFileName); bool ShowImageByBackBuffer(const char* pbSrcData,int iSrcDataSize); private: LPDIRECT3D9 m_pD3D; LPDIRECT3DDEVICE9 m_pD3D_Device; LPDIRECT3DVERTEXBUFFER9 m_pD3D_VB; LPDIRECT3DTEXTURE9 m_pD3D_Texture; }; #endif
// DxShowImage.cpp
#include "stdafx.h" // for MFC #include "DxShowImage.h" #include <d3dx9math.h> #pragma comment(lib,"d3d9.lib") #pragma comment(lib,"d3dx9.lib") CDxShowImage::CDxShowImage() { m_pD3D = NULL; m_pD3D_Device = NULL; m_pD3D_VB = NULL; m_pD3D_Texture = NULL; } CDxShowImage::~CDxShowImage() { Uninit(); } bool CDxShowImage::Init(HWND hWnd) { D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp,sizeof(d3dpp)); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; m_pD3D = Direct3DCreate9(D3D_SDK_VERSION); if ( NULL == m_pD3D ) { Uninit(); return false; } if ( Failed(m_pD3D->CreateDevice( D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&m_pD3D_Device )) ) { Uninit(); return false; } m_pD3D_Device->SetRenderState(D3DRS_LIGHTING,FALSE); m_pD3D_Device->SetSamplerState(0,D3DSAMP_ADDRESSU,D3DTADDRESS_BORDER); m_pD3D_Device->SetSamplerState(0,D3DSAMP_ADDRESSV,D3DTADDRESS_BORDER); CUSTOMVERTEX rgVertices[] = { { -1.0f,-1.0f,0.0f,1.0f },{ -1.0f,1.0f,0.0f },{ 1.0f,}; if ( Failed(m_pD3D_Device->CreateVertexBuffer( sizeof(rgVertices),D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT,&m_pD3D_VB,NULL )) ) { Uninit(); return false; } VOID* pVertices = NULL; if ( Failed(m_pD3D_VB->Lock(0,sizeof(rgVertices),(void**)&pVertices,0)) ) { Uninit(); return false; } memcpy(pVertices,rgVertices,sizeof(rgVertices)); m_pD3D_VB->Unlock(); return true; } void CDxShowImage::Uninit() { SAFE_RELEASE(m_pD3D_Texture); SAFE_RELEASE(m_pD3D_VB); SAFE_RELEASE(m_pD3D_Device); SAFE_RELEASE(m_pD3D); } bool CDxShowImage::ShowImageByTexture(const char* szFileName) { bool fRet = false; if ( m_pD3D_Device ) { m_pD3D_Device->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(255,255,255),0); if ( SUCCEEDED( m_pD3D_Device->BeginScene() ) ) { if ( SUCCEEDED(D3DXCreateTextureFromFile(m_pD3D_Device,szFileName,&m_pD3D_Texture)) ) { m_pD3D_Device->SetTexture(0,m_pD3D_Texture); m_pD3D_Device->SetStreamSource(0,m_pD3D_VB,sizeof(CUSTOMVERTEX)); m_pD3D_Device->SetFVF(D3DFVF_CUSTOMVERTEX); m_pD3D_Device->DrawPrimitive(D3DPT_TRIANGLESTRIP,2); fRet = true; } m_pD3D_Device->EndScene(); } m_pD3D_Device->Present(NULL,NULL); } return fRet; } bool CDxShowImage::ShowImageByTexture(const char* pbSrcData,int iSrcDataSize) { bool fRet = false; if ( m_pD3D_Device ) { m_pD3D_Device->Clear(0,0); if ( SUCCEEDED( m_pD3D_Device->BeginScene() ) ) { if ( SUCCEEDED(D3DXCreateTextureFromFileInMemory(m_pD3D_Device,pbSrcData,iSrcDataSize,NULL); } return fRet; } bool CDxShowImage::ShowImageBySurface(const char* szFileName) { bool fRet = false; IDirect3DSurface9* pRenderTarget = NULL; if ( S_OK == m_pD3D_Device->GetRenderTarget(0,&pRenderTarget) ) { m_pD3D_Device->Clear(0,0); if ( S_OK == D3DXLoadSurfaceFromFile( pRenderTarget,D3DX_FILTER_NONE,NULL) ) { m_pD3D_Device->Present(NULL,NULL); fRet = true; } SAFE_RELEASE(pRenderTarget); } return fRet; } bool CDxShowImage::ShowImageBySurface(const char* pbSrcData,int iSrcDataSize) { bool fRet = false; IDirect3DSurface9* pRenderTarget = NULL; if ( S_OK == m_pD3D_Device->GetRenderTarget(0,0); if ( S_OK == D3DXLoadSurfaceFromFileInMemory( pRenderTarget,NULL); fRet = true; } SAFE_RELEASE(pRenderTarget); } return fRet; } bool CDxShowImage::ShowImageByOffscreen(const char* szFileName,int h) { bool fRet = false; IDirect3DSurface9* pRenderTarget = NULL; if ( S_OK == m_pD3D_Device->CreateOffscreenPlainSurface( w,h,D3DFMT_X8R8G8B8,&pRenderTarget,NULL) ) { if ( S_OK == D3DXLoadSurfaceFromFile( pRenderTarget,NULL) ) { IDirect3DSurface9* backBuffer = NULL; if ( S_OK == m_pD3D_Device->GetBackBuffer(0,D3DBACKBUFFER_TYPE_MONO,&backBuffer) ) { m_pD3D_Device->Clear(0,0); m_pD3D_Device->StretchRect(pRenderTarget,backBuffer,D3DTEXF_NONE); m_pD3D_Device->Present(NULL,NULL); SAFE_RELEASE(backBuffer); fRet = true; } } SAFE_RELEASE(pRenderTarget); } return fRet; } bool CDxShowImage::ShowImageByOffscreen(const char* pbSrcData,NULL) ) { if ( S_OK == D3DXLoadSurfaceFromFileInMemory( pRenderTarget,NULL); SAFE_RELEASE(backBuffer); fRet = true; } } SAFE_RELEASE(pRenderTarget); } return fRet; } bool CDxShowImage::ShowImageByBackBuffer(const char* szFileName) { bool fRet = false; IDirect3DSurface9* backBuffer = NULL; if ( S_OK == m_pD3D_Device->GetBackBuffer(0,&backBuffer) ) { m_pD3D_Device->Clear(0,0); if ( S_OK == D3DXLoadSurfaceFromFile( backBuffer,NULL); fRet = true; } SAFE_RELEASE(backBuffer); } return fRet; } bool CDxShowImage::ShowImageByBackBuffer(const char* pbSrcData,int iSrcDataSize) { bool fRet = false; IDirect3DSurface9* backBuffer = NULL; if ( S_OK == m_pD3D_Device->GetBackBuffer(0,0); if ( S_OK == D3DXLoadSurfaceFromFileInMemory( backBuffer,NULL); fRet = true; } SAFE_RELEASE(backBuffer); } return fRet; }