Take screenshot with directX?

This is a discussion on Take screenshot with directX? within the Game Programming forums, part of the General Programming Boards category; Hi, I am trying to take a screenshot using directX. I have tried to compile some samples and read some ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    18

    Question Take screenshot with directX?

    Hi, I am trying to take a screenshot using directX.
    I have tried to compile some samples and read some articles but I can´t get it to work.

    The problem is the 2 functions that saves the surface to a file, SurfaceLoader.Save() and D3DXSaveSurfaceToFile().

    When I try to use surfaceloader.save I get the error: “error: `SurfaceLoader' undeclared (first use this function)”.

    And then the function D3DXSaveSurfaceToFile that almost all uses in there examples and articles, when I try to use it I get the error: “main.cpp: undefined reference to `D3DXSaveSurfaceToFileA@20'”

    I hope that someone can tell me how to get one of the functions to work, or how to work around the problem. like saving the surface to a GDI's BITMAP and then save it.

    Code:
    #include <windows.h>
    #include <iostream.h>
    #include <dx9/d3d9.h>
    #include <dx9/d3dx9.h>
    #include <dx9/D3dx9tex.h>
    
    IDirect3DDevice9*	g_pd3dDevice=NULL;
    IDirect3DSurface9*	g_pSurface=NULL;
    
    using namespace std;
    
    int WINAPI WinMain (HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nFunsterStil)
    
    {
        g_pd3dDevice->CreateOffscreenPlainSurface(200, 300, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &g_pSurface, NULL);
        g_pd3dDevice->GetFrontBufferData(0, g_pSurface);
        //this
        SurfaceLoader.Save("Screenshot.bmp", 0, g_pSurface);
        //or???
        D3DXSaveSurfaceToFile("Screenshot.bmp",D3DXIFF_BMP,g_pSurface,NULL,NULL);
        
        g_pSurface->Release();
        return 0;
    }
    EDIT:
    I use: DEV-C++, C++, dxsdk_aug2005
    Last edited by Dampy; 05-25-2008 at 06:59 AM. Reason: Forgot

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    SurfaceLoader is not defined anywhere.
    You can't use something of which you haven't created. That simple.
    That is why you get that compile error.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    I think he has XNA and DX mixed up in the same code. D3DXSaveTextureToFile() will work.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    18
    Quote Originally Posted by Elysia View Post
    SurfaceLoader is not defined anywhere.
    You can't use something of which you haven't created. That simple.
    That is why you get that compile error.
    But SurfaceLoader is a class/function in dx?
    http://msdn.microsoft.com/en-us/libr...30(VS.85).aspx
    So, how do I define in?

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    I don't know how it works (where it's located and if it's managed or whatever). But Bubba did point out that D3DXSaveTextureToFile will work, so use that instead then.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    18
    Quote Originally Posted by Bubba View Post
    I think he has XNA and DX mixed up in the same code. D3DXSaveTextureToFile() will work.

    I get the same error :/
    "undefined reference to `D3DXSaveTextureToFileA@16'"

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    Did you link against the correct lib?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    18
    Quote Originally Posted by Elysia View Post
    Did you link against the correct lib?
    Yes
    I tried to add all the libs to my project and i even tried to use the "unicode version", but I get the same error.

    This is one of the articles: http://www.codeproject.com/KB/dialog/screencap.aspx

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    From what I understand, you must link with d3dx9.lib. Make sure you do that and that it's the correct version.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    18
    Now I have installed the directX Dev-pack for dev-c++, and the samples works fine and my program compiles as it should.
    But, when I try to start my program it crashes.

    When I debug it I get the error: "An access volation (Segmentation Fault) raised in your program" and highlights the row “g_pd3dDevice->CreateOffscreenPlainSurface(“
    When I comment out all the "->" rows the program works fine, I think is something whit "g_pd3dDevice->".

    I find the code in a other forum, and it seems to work for all the outers
    I am probably doing something wrong, so if someone can take a last look =)

    Code:
    #include <d3d9.h>
    #include <d3dx9.h>
    //#include <D3dx9tex.h>
    #include <windows.h>
    //#pragma comment(lib,"d3d9.lib")
    //#pragma comment(lib,"d3dx9.lib")
    
    IDirect3DDevice9*	g_pd3dDevice=NULL;
    IDirect3DSurface9*	g_pSurface=NULL;
    
    bool SaveScreenShot( char *fileName, _D3DXIMAGE_FILEFORMAT ImgFormat);
    
    INT WINAPI WinMain( HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow )
    {
        
        SaveScreenShot("test.bmp",D3DXIFF_BMP);
        //g_pd3dDevice->CreateOffscreenPlainSurface(200, 300, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &g_pSurface, NULL);
        //g_pd3dDevice->GetFrontBufferData(0, g_pSurface);
        //g_pd3dDevice->CreateTexture(200,300,0,0,D3DFMT_A8R8G8B8,D3DPOOL_SCRATCH, &g_pTexture,NULL);
        //this
        //SurfaceLoader.Save("Screenshot.bmp", 0, g_pSurface);
        //or???
        //D3DXSaveSurfaceToFile("Screenshot.bmp",D3DXIFF_BMP,g_pSurface,NULL,NULL);
        //g_pSurface->Release();
        return 0;
    }
    
    bool SaveScreenShot( char *fileName, _D3DXIMAGE_FILEFORMAT ImgFormat)
    {
    	HRESULT hr;	
    	
    	//The front buffer used to get the screenshot	
    	LPDIRECT3DSURFACE9 buffer = NULL; 
    	
    	
    	// Create a surface for the screenshot to be rendered to	
    	if( FAILED( hr = g_pd3dDevice->CreateOffscreenPlainSurface( 
    		1024,
    		768,	
    		D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &buffer, NULL ) ) )		
    		return false;	
    	
    	// Write the front buffer to the surface 	
    	if( FAILED( hr = g_pd3dDevice->GetFrontBufferData( 0, buffer ) ) )
    		{			
    			//mGCLog.Error("Cannot Acquire front buffer...");			
    			buffer->Release();			
    			
    			return false;		
    		}	
    				
    	D3DXSaveSurfaceToFile( fileName, ImgFormat, buffer, NULL, NULL );	
    	
    	// Cleanup	
    	buffer->Release();		
    	return true;
    }

  11. #11
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Not sure if you should be releasing the buffer if it failed? I could be wrong

    http://msdn.microsoft.com/en-us/libr...88(VS.85).aspx

    Take a look and read.

  12. #12
    Registered User
    Join Date
    Sep 2007
    Posts
    18
    Yes, you probably have right, but I donīt think it is something to do with bad code. or?
    because I can start a program that looks like this:

    Code:
    #include <d3d9.h>
    #include <d3dx9.h>
    #include <windows.h>
    
    IDirect3DDevice9*	g_pd3dDevice=NULL;
    IDirect3DSurface9*	g_pSurface=NULL;
    
    INT WINAPI WinMain( HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow )
    {
        return 0;
    }
    But when I try to start this it crashes:

    Code:
    #include <d3d9.h>
    #include <d3dx9.h>
    #include <windows.h>
    
    IDirect3DDevice9*	g_pd3dDevice=NULL;
    IDirect3DSurface9*	g_pSurface=NULL;
    
    INT WINAPI WinMain( HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow )
    {
        g_pd3dDevice->CreateOffscreenPlainSurface(200, 300, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &g_pSurface, NULL);
        g_pSurface->Release();
        return 0;
    }
    I probably need to turn to a devc++ forum now? If someone canīt find anything.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    But you haven't created any devices yet!
    You haven't assigned any valid pointers to g_pd3dDevice and g_pSurface!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  14. #14
    Registered User
    Join Date
    Sep 2007
    Posts
    18
    Thanks for pointing me on the right way, now it&#180;s working like a glove =)

    But I have 3 miner issues I hopped if someone can point me to a solution:

    I am trying to take sceenshots in a game that uses dx. In fullscreen the screenshots only gets black, but in windowmode it&#180;s working fine, why?

    When I running a game that using dx, I can&#180;t start my program.
    It fails on:
    Code:
    if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING ,&d3dpp,&g_pd3dDevice)))
    	{
    		MessageBox(NULL, "Unable to Create Device", "Error", MB_OK | MB_ICONEXCLAMATION);
    		return E_FAIL;
    	}
    If I start my program first then starts the game, it&#180;s working fine, but if I shutdown my program and try to start I again it fails? If o shutdown the game then my program works again.

    And last up.
    I read that getfrontbufferdata is very slow, and it&#180;s. The pictures get all messed up if it&#180;s something moving on the screen. Is there a better way to do it? Or maybe a faster way to get a whole frame?
    Last edited by Dampy; 05-27-2008 at 07:26 AM. Reason: spelling

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    I don't think it's a very good idea to use software processing of any kind. But I don't know if DX requires exclusive access either...
    Every card today supports hardware processing which is like 1000x times faster.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX screenshot via C (app crashes)
    By Delusionz in forum C Programming
    Replies: 6
    Last Post: 01-11-2009, 08:55 AM
  2. Isometric Tile Engine using DirectX
    By Wraithan in forum Game Programming
    Replies: 3
    Last Post: 07-17-2006, 12:16 PM
  3. DirectSound header issues
    By dxfoo in forum C++ Programming
    Replies: 0
    Last Post: 03-19-2006, 06:16 PM
  4. DirectX - Starting Guide?
    By Zeusbwr in forum Game Programming
    Replies: 13
    Last Post: 11-24-2004, 11:49 PM
  5. Directx SDK Documentation
    By Zoalord in forum Game Programming
    Replies: 4
    Last Post: 05-08-2003, 06:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21