1: HRESULT blTakeScreenShot()
2: {
3: HRESULT hr = S_OK;
4: LPDIRECT3DSURFACE9 frontbuf;
5: char filename[64];
6: FILE* f;
7: int x, y;
8: RECT rcWindow;
9:
10: // look for the next free file
11: for(int i=0;i<999;i++)
12: {
13: // build the filename
14: sprintf(filename, "screen%.3d.bmp", i);
15: f = fopen(filename, "r");
16: if( f == NULL)
17: break;
18: else
19: fclose( f );
20: }
21: // get the screen resolution. If it's a windowed application get the whole
22: // screen size. If it's a fullscreen application you might have somewhere
23: // your defines as: #define SCREEN_WIDTH 800
24: if( !g_bWindowed )
25: {
26: x = SCREEN_WIDTH;
27: y = SCREEN_HEIGHT;
28: }
29: else
30: {
31: x = GetSystemMetrics( SM_CXSCREEN );
32: y = GetSystemMetrics( SM_CYSCREEN );
33: // to get the window sizes
34: GetWindowRect( g_hWnd, &rcWindow );
35: }
36: // here we create an empty Surface. The parameter D3DFMT_A8R8G8B8 creates an 32 bit image with
37: // an alpha channel and 8 bits per channel.
38: if( FAILED( hr = g_pd3dDevice->CreateOffscreenPlainSurface(x, y, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &frontbuf, NULL)))
39: return hr;
40: // now we copy the entire frontbuffer into our new surface. The first parameter is NULL since
41: // we assume we have only one swap chain
42: if( FAILED( hr = g_pd3dDevice->GetFrontBufferData(NULL, frontbuf)))
43: {
44: // if this fails release our surface so we have no memory leak
45: frontbuf->Release();
46: return hr;
47: }
48: // This is the most important functions. The DirectX-SDK provides this handy little function to
49: // save our surface to a file. The first parameter is our specified filename, the second parameter
50: // tells DirectX what kind of file we want to save (in this example we decide to save to BMP)
51: // Note the difference between a fullscreen screenshot and a windowed one. If we have a windowed application
52: // we only want the specified RECT saved from our screen capture
53: if( !g_bWindowed )
54: D3DXSaveSurfaceToFile(filename, D3DXIFF_BMP, frontbuf, NULL, NULL);
55: else
56: D3DXSaveSurfaceToFile(filename, D3DXIFF_BMP, frontbuf, NULL, &rcWindow);
57:
58: frontbuf->Release();
59: return hr;
60: }