Reidar Lange
Newcomer
- Joined
- Oct 29, 2004
- Messages
- 16
Screenshot from directX in C#. SOLVED!
In c++ there is a nice way of geting and saving a screenshot. Does anyone know how to do this in C#. The problem is that ther is no equivalent to the C++ D3DXSaveSurfaceToFile() in C#.
I have found som C++ code that gos like this:
HRESULT CMyD3DApplication::Screenshot( char* filename )
{
HRESULT hr;
D3DDISPLAYMODE dmCurrent;
LPDIRECT3DSURFACE9 pSurface = NULL;
if ( !m_pd3dDevice ) return E_FAIL;
/////////////////////////////////
// QUERY CURRENT WINDOW MODE //
/////////////////////////////////
hr = m_pd3dDevice->GetDisplayMode( 0, &dmCurrent );
if ( FAILED( hr ) )
{
return( hr );
}
////////////////////////////////////////
// CREATE SUFACE SAME SIZE AS WINDOW //
////////////////////////////////////////
hr = m_pd3dDevice->CreateOffscreenPlainSurface( dmCurrent.Width, dmCurrent.Height,
D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH,
&pSurface, NULL );
if ( FAILED( hr ) )
{
return( hr );
}
//////////////////////////////////////////////////////////////////
// COPY FRONTBUFFER INTO SURFACE (WE CANT TOUCH IT DIRECTLY) //
//////////////////////////////////////////////////////////////////
hr = m_pd3dDevice->GetFrontBufferData( 0, pSurface );
if ( FAILED( hr ) )
{
pSurface->Release();
return( hr );
}
//////////////////////////////
// OUTPUT SURFACE TO BMP //
//////////////////////////////
hr = D3DXSaveSurfaceToFile( filename, D3DXIFF_BMP, pSurface, NULL, NULL );
pSurface->Release();
return hr;
}
The C# version will look like this:
Surface renderTarget = null;
Surface destTarget = null;
renderTarget = device.GetRenderTarget(0);
destTarget = device.CreateOffscreenPlainSurface(ClientRectangle.Width,ClientRectangle.Height,graphicsSettings.WindowedDisplayMode.Format,Pool.SystemMemory);
device.GetRenderTargetData(renderTarget,destTarget);
// Now we are ready to save, but how ?????????
In c++ there is a nice way of geting and saving a screenshot. Does anyone know how to do this in C#. The problem is that ther is no equivalent to the C++ D3DXSaveSurfaceToFile() in C#.
I have found som C++ code that gos like this:
HRESULT CMyD3DApplication::Screenshot( char* filename )
{
HRESULT hr;
D3DDISPLAYMODE dmCurrent;
LPDIRECT3DSURFACE9 pSurface = NULL;
if ( !m_pd3dDevice ) return E_FAIL;
/////////////////////////////////
// QUERY CURRENT WINDOW MODE //
/////////////////////////////////
hr = m_pd3dDevice->GetDisplayMode( 0, &dmCurrent );
if ( FAILED( hr ) )
{
return( hr );
}
////////////////////////////////////////
// CREATE SUFACE SAME SIZE AS WINDOW //
////////////////////////////////////////
hr = m_pd3dDevice->CreateOffscreenPlainSurface( dmCurrent.Width, dmCurrent.Height,
D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH,
&pSurface, NULL );
if ( FAILED( hr ) )
{
return( hr );
}
//////////////////////////////////////////////////////////////////
// COPY FRONTBUFFER INTO SURFACE (WE CANT TOUCH IT DIRECTLY) //
//////////////////////////////////////////////////////////////////
hr = m_pd3dDevice->GetFrontBufferData( 0, pSurface );
if ( FAILED( hr ) )
{
pSurface->Release();
return( hr );
}
//////////////////////////////
// OUTPUT SURFACE TO BMP //
//////////////////////////////
hr = D3DXSaveSurfaceToFile( filename, D3DXIFF_BMP, pSurface, NULL, NULL );
pSurface->Release();
return hr;
}
The C# version will look like this:
Surface renderTarget = null;
Surface destTarget = null;
renderTarget = device.GetRenderTarget(0);
destTarget = device.CreateOffscreenPlainSurface(ClientRectangle.Width,ClientRectangle.Height,graphicsSettings.WindowedDisplayMode.Format,Pool.SystemMemory);
device.GetRenderTargetData(renderTarget,destTarget);
// Now we are ready to save, but how ?????????
Last edited: