XNA Creators Club Online
community forums
Page 1 of 1 (3 items)
Sort Posts: Previous Next

screenshot?

Last post 1/19/2009 4:22 PM by getviewsnow. 2 replies.
  • 11/27/2008 5:35 PM

    screenshot?

    I were checking online how to make screen shot in direct x 9 and I found something like this:
    extern IDirect3DDevice9* pd3dDevice;
    HRESULT WINAPI CaptureScreen()
    {
         HRESULT hr;
        IDirect3DSurface9* pSurface;
        pd3dDevice->CreateOffscreenPlainSurface(800, 600, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &pSurface, NULL);
        pd3dDevice->GetFrontBufferData(0, pSurface);
        D3DXSaveSurfaceToFile(L"something.bmp",D3DXIFF_BMP,pSurface,NULL,NULL);
        pSurface->Release();

        return hr;
    }

    When I put this in any sample in DirectX SDK which uses DXUT, application crashes and debugger points on line:
    pd3dDevice->CreateOffscreenPlainSurface(800, 600, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &pSurface, NULL);

    I guess its something wrong with pd3dDevice, but I have no idea what could that be.


  • 11/28/2008 8:39 PM In reply to

    Re: screenshot?

    From the way this code is written, its a bit hard to determine exactly what is causing the error.  The parameters you are passing to the create surface function looks ok, for the most part.  If you were running the program in full screen mode, you could use something like GetSystemMetrics to get the dimensions of the screen to capture, or get from the window, if in windowed mode, so you are not hardcoding to a specific size.

    It may also be that you don't have the interface for your device.  I have functions written to do screen caps (example below), and in my game app, I have the device interface that I can use.  If you want you could add some code to check for a valid device before attempting to create the offscreen surface.

     

    void CGameApp::ScreenCapture(char *fileName)  
    {  
        LPDIRECT3DSURFACE9 surface;  
     
        // get display dimensions  
        int cx = GetSystemMetrics(SM_CXSCREEN);  
        int cy = GetSystemMetrics(SM_CYSCREEN);  
     
        // create image surface to store the image  
        if(FAILED(m_pD3DDevice->CreateOffscreenPlainSurface(cx, cy, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM,  
            &surface, NULL)))  
        {  
            MessageBox(NULL, _T("CreateOffscreenPlainSurface() failed!"), _T("Error"), MB_OK);  
            return;  
        }  
     
        // read the front buffer  
        if(FAILED(m_pD3DDevice->GetFrontBufferData(0, surface)))  
        {  
            MessageBox(NULL, _T("GetFrontBufferData() failed!"), _T("Error"), MB_OK);  
            surface->Release();  
            return;  
        }  
     
        // write the surface to file  
        if(FAILED(D3DXSaveSurfaceToFile(fileName, D3DXIFF_BMP, surface, NULL, NULL)))  
        {  
            MessageBox(NULL, _T("D3DXSaveSurfaceToFile() failed!"), _T("Error"), MB_OK);  
            surface->Release();  
        }  

     

     

     

  • 1/19/2009 4:22 PM In reply to

    Re: screenshot?

    Ok, Ive found where the problem was. The device didn't work as it should, I fixed that, and now device isn't NULL anymore. It makes screenshot, but the problem is, its black. I get something.bmp, but its black, which is another frustrating problem I have.

    I guess there is something wrong with surface...

    Here is the code:
    HRESULT WINAPI CaptureScreen(IDirect3DDevice9* pd3dDevice) 
        IDirect3DSurface9* pSurface; 
        //IDirect3DDevice9 *pd3dDevice = DXUTGetD3D9Device(); 
     
            
           if (pd3dDevice != NULL) 
            { 
        //DXUTShutdown( 0 ); //proof device isnt NULL     
        pd3dDevice->CreateOffscreenPlainSurface(1024, 768, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &pSurface, NULL); 
        pd3dDevice->GetFrontBufferData(0, pSurface); 
            } 
        D3DXSaveSurfaceToFile(L"Media\\Screenshots\\something.bmp",D3DXIFF_BMP,pSurface,NULL,NULL); 
        pSurface->Release();  
     
        return 0; 
     

Page 1 of 1 (3 items) Previous Next