HRESULT blTakeScreenShot()
{
HRESULT hr = S_OK;
LPDIRECT3DSURFACE9 frontbuf;
char filename[64];
FILE* f;
int x, y;
RECT rcWindow;
// look for the next free file
for(int i=0;i<999;i++)
{
// build the filename
sprintf(filename, "screen%.3d.bmp", i);
f = fopen(filename, "r");
if( f == NULL)
break;
else
fclose( f );
}
// get the screen resolution. If it's a windowed application get the whole
// screen size. If it's a fullscreen application you might have somewhere
// your defines as: #define SCREEN_WIDTH 800
if( !g_bWindowed )
{
x = SCREEN_WIDTH;
y = SCREEN_HEIGHT;
}
else
{
x = GetSystemMetrics( SM_CXSCREEN );
y = GetSystemMetrics( SM_CYSCREEN );
// to get the window sizes
GetWindowRect( g_hWnd, &rcWindow );
}
// here we create an empty Surface. The parameter D3DFMT_A8R8G8B8 creates an 32 bit image with
// an alpha channel and 8 bits per channel.
if( FAILED( hr = g_pd3dDevice->CreateOffscreenPlainSurface(x, y, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &frontbuf, NULL)))
return hr;
// now we copy the entire frontbuffer into our new surface. The first parameter is NULL since
// we assume we have only one swap chain
if( FAILED( hr = g_pd3dDevice->GetFrontBufferData(NULL, frontbuf)))
{
// if this fails release our surface so we have no memory leak
frontbuf->Release();
return hr;
}
// This is the most important functions. The DirectX-SDK provides this handy little function to
// save our surface to a file. The first parameter is our specified filename, the second parameter
// tells DirectX what kind of file we want to save (in this example we decide to save to BMP)
// Note the difference between a fullscreen screenshot and a windowed one. If we have a windowed application
// we only want the specified RECT saved from our screen capture
if( !g_bWindowed )
D3DXSaveSurfaceToFile(filename, D3DXIFF_BMP, frontbuf, NULL, NULL);
else
D3DXSaveSurfaceToFile(filename, D3DXIFF_BMP, frontbuf, NULL, &rcWindow);
frontbuf->Release();
return hr;
}