Screenshot of game window

Started by
2 comments, last by EddieV223 10 years, 8 months ago

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;
    }
Advertisement

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/

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

i wont a screenshot of a different game

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

If this post or signature was helpful and/or constructive please give rep.

// C++ Video tutorials

http://www.youtube.com/watch?v=Wo60USYV9Ik

// Easy to learn 2D Game Library c++

SFML2.2 Download http://www.sfml-dev.org/download.php

SFML2.2 Tutorials http://www.sfml-dev.org/tutorials/2.2/

// Excellent 2d physics library Box2D

http://box2d.org/about/

// SFML 2 book

http://www.amazon.com/gp/product/1849696845/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=1849696845&linkCode=as2&tag=gamer2creator-20

This topic is closed to new replies.

Advertisement