Sign in to follow this  

Screenshot of game window

Recommended Posts

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;
        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;
                                       D3DDEVTYPE_HAL, hWnd,
                                       &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);
        return 0;

Share this post

Link to post
Share on other sites

Wait, what do you want to take the screenshot from? Your own game? Or a different game?


If it's your own game you should have a CreateWindow call somewhere, it would be that HWND.


If it's a different executable you can't do it simply like that. You have to hook into Direct3d. Here's a sample code, going from there should work easily:

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this