I know how to use GDI to capture screen, however it is very slow (it barely captures 10 fps)
I have read that DirectX offers the best speed. But before I start learning DirectX I wanted to test a sample to see if it is really that fast.
I have found this question that offers a sample code to do that:
void dump_buffer()
{
IDirect3DSurface9* pRenderTarget=NULL;
IDirect3DSurface9* pDestTarget=NULL;
const char file[] = "Pickture.bmp";
// sanity checks.
if (Device == NULL)
return;
// get the render target surface.
HRESULT hr = Device->GetRenderTarget(0, &pRenderTarget);
// get the current adapter display mode.
//hr = pDirect3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddisplaymode);
// create a destination surface.
hr = Device->CreateOffscreenPlainSurface(DisplayMde.Width,
DisplayMde.Height,
DisplayMde.Format,
D3DPOOL_SYSTEMMEM,
&pDestTarget,
NULL);
//copy the render target to the destination surface.
hr = Device->GetRenderTargetData(pRenderTarget, pDestTarget);
//save its contents to a bitmap file.
hr = D3DXSaveSurfaceToFile(file,
D3DXIFF_BMP,
pDestTarget,
NULL,
NULL);
// clean up.
pRenderTarget->Release();
pDestTarget->Release();
}
I have tried to include the required files. However not all of them can be included (for example #include <D3dx9tex.h>
).
Can anyone provide a working example that has all of the required includes or point me to what libraries I should install.
I am using Visual C++ 2010 Express on Windows 7 Ultimate (x64).
Edit:
Also, this code is not complete, for example what is the Device
identifier?!
You have not stated the requirements for the target Windows versions. If you do not need to support Windows 7, Windows 8 includes a nice new DXGI interface IDXGIOutputDuplication that allows to create a COM object that duplicates the output of a video adapter and provides CPU access to the video memory through IDXGIOutputDuplication::MapDesktopSurface. MSDN has quite a nice sample that captures the desktop through this and draws it inside a form and works nice and smooth. So if Windows 7 is not a must have, I'd suggest you look at this.
You can get the DirectX SDK from microsoft.com/en-ca/download/details.aspx?id=6812 (posted by @yms). This SDK is compatible with all versions Windows, including XP. Refer to its documentation on how to include/link with D3D9.
In your example
Device
is anIDirect3DDevice9
. Every D3D9 application must create one of these. It's very easy to find example code on how to create one (eg. https://msdn.microsoft.com/en-us/library/windows/desktop/bb204867%28v=vs.85%29.aspx).In your example code, only the contents being rendered in DirectX are being captured, which I assume is not your intention. To capture the entire screen (which I'm assuming is the goal), instead of using
IDirect3DDevice9::GetRenderTarget
, you should useIDirect3DDevice9::GetFrontBufferData
, as in this tutorial (http://dim-i.net/2008/01/29/taking-screenshots-with-directx-and-dev-cpp/). If you're looking for speed, you should not recreate the offscreen surface every frame, as in both your example and this tutorial. The memory pool should beD3DPOOL_SYSTEMMEM
in this case, notD3DPOOL_SCRATCH
. Depending on the size of the screen, the likely bottleneck will be writing the images to disk.Also note that the screen captured from this will be for the adapter used to create the
IDirect3DDevice9
. This is the first parameter toIDirect3D9::CreateDevice
. This is only a concern if capturing multiple monitors is a possibility.Here is some sample code to capture the screen with DirectX 9. You shouldn't have to install any SDK (except the standard files that come with Visual Studio, although I didn't tested VS 2010).
Just create a simple Win32 console application, add the following in the stdafx.h file:
Here is the sample main implementation
What this will do is capture 10 times the screen, and save "cap%i.png" images on the disk. It will also display the time taken for this (saving images is not counted in that time, only screen captures). On my (desktop windows 8 - Dell Precision M2800/i7-4810MQ-2.80GHz/Intel HD 4600 which is a pretty crappy machine...) machine, it takes 100 1920x1080 captures in ~4sec, so around 20/25 fps.
Note this code implicitely links to WIC (an imaging library included with Windows for quite a time now) to save the image files (so you don't need the famous D3DXSaveSurfaceToFile that require old DirectX SDKs to be installed):
And some macros I used:
Note: for Windows 8+ clients, all these (except WIC) should be dropped in favor of the Desktop Duplication API.