The original article written for Visual Basic can be found at
With the release of the DirectX 8.1 SDK, new functions were introduced to the D3DX libraries providing pre-canned routines to write image files from DirectX resources. These functions allow surface, texture, and volume resources to be stored to disk as either BMP or DDS files. In this article, we will examine a function to save a screen shot from a VC++ application using the DirectX 8.1 SDK.
In previous versions of DirectX, we might have locked the primary surface and read the pixel data from the frame buffer. However, as of the release of DirectX 8, direct access to the primary surface memory is no longer available. Instead, the GetFrontBuffer() provides a facility to retrieve the contents of the frame buffer to an image surface in system memory. Format conversion is automatically performed, converting from the current display format to a fixed format of A8R8G8B8.
Our function will take two parameters, a pointer to the current device and a pointer to a null terminated string containing the file name of the BMP to be created. The function will return D3D_OK on success, or pass the error code returned by the last function on failure. The first thing that we must do is create an image surface the size of the display, with a format of D3DFMT_A8R8G8B8:
Next, this surface is passed to the GetFrontBuffer() method of the device, which will copy the entire screen into our image buffer:
Finally, we call D3DXSaveSurfaceToFile() to create the BMP file, and release the temporary image surface:
Visitors Since 1/1/2000: