4

How can i take a screenshot of a full screen game in c++? I know it can be done with directx and i have written a simple code which can take a screenshot of a window but i don't know how to get a HWND of game. Here is my code.

#include <d3dx9.h>
#include <d3dx9tex.h>
#include <stdio.h>

#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")

int main()
{
    IDirect3DSurface9 *surface;
    IDirect3DDevice9 *g_pd3dDevice;
    IDirect3D9 *g_pD3D;
    D3DDISPLAYMODE d3ddm;
    D3DPRESENT_PARAMETERS d3dpp;
    HWND hWnd = GetConsoleWindow();

    if((g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
    {
        printf("fail 1");
    }

    if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
    {
        printf("fail 2");
    }

    ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));

    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = d3ddm.Format;
    d3dpp.BackBufferHeight = d3ddm.Height;
    d3dpp.BackBufferWidth = d3ddm.Width;

    if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,
                                   D3DDEVTYPE_HAL, hWnd,
                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                   &d3dpp, &g_pd3dDevice)))
    {
        printf("fail 3");
    }
    g_pd3dDevice->CreateOffscreenPlainSurface(800, 600, D3DFMT_A8R8G8B8,     D3DPOOL_SCRATCH, &surface, NULL);
    g_pd3dDevice->GetFrontBufferData(0, surface);
    D3DXSaveSurfaceToFile("c:\\tmp\\output.jpg", D3DXIFF_JPG, surface, NULL, NULL);
    surface->Release();
    return 0;
}
psych
  • 41
  • 1
  • 2
  • I've found the answer on Stackoverflow: http://stackoverflow.com/questions/3291167/how-to-make-screen-screenshot-with-win32-in-c –  Jul 31 '13 at 07:21
  • but is it working for games? – psych Jul 31 '13 at 07:24
  • 1
    @Xobdrop That doesn't answer the question. It does not capture certain windows, and it won't capture DirectX rendered windows at all (they will appear as black boxes) – nikolas Jul 31 '13 at 07:25
  • and so what i gonna do? – psych Jul 31 '13 at 07:40
  • http://stackoverflow.com/questions/4120108/how-to-save-backbuffer-to-file-in-directx-10 This should help you. It tells you how to render to a file. – David Aug 05 '13 at 06:41

1 Answers1

1

I don't have any experience with Direct3D, but with OpenGL. But both APIs provide almost the same functionality.

You should read back the back buffer (using Direct3D and not some Windows functionality) to host memory. Then simply save that bitmap to a file using an image library or something like that.

It's actually an extremely simple process.

These two links might be helpful to you:
http://www.mvps.org/directx/articles/screengrab.htm
How to save backbuffer to file in DirectX 10?

Community
  • 1
  • 1
Tara
  • 1,673
  • 22
  • 30