Jump to content
  • Advertisement
Sign in to follow this  

Screenshot of game window

This topic is 2181 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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: http://dxresearch.codeplex.com/

Share this post

Link to post
Share on other sites

Do you have to do it in code?  If not just push print screen button on keyboard and paste into any image editor.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!