我知道如何使用 GDI 来捕获屏幕,但是它非常慢(它几乎不能捕获 10 fps)
我读过 DirectX 提供了最好的速度。但在我开始学习 DirectX 之前,我想测试一个样本,看看它是否真的那么快。
我发现这个 问题 提供了一个示例代码来做到这一点:
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();
}
我试图包含所需的文件。但是,并非所有这些都可以包括在内(例如 #include <D3dx9tex.h>
)。
任何人都可以提供一个包含所有必需包含的工作示例或指向我应该安装哪些库。
我在 Windows 7 Ultimate (x64) 上使用 Visual C++ 2010 Express。
编辑:
另外,这段代码并不完整,例如 Device
标识符是什么?!
原文由 user4592590 发布,翻译遵循 CC BY-SA 4.0 许可协议
这是一些使用 DirectX 9 捕获屏幕的示例代码。您不必安装任何 SDK(Visual Studio 附带的标准文件除外,尽管我没有测试过 VS 2010)。
只需创建一个简单的 Win32 控制台应用程序,在 stdafx.h 文件中添加以下内容:
这是示例主要实现
这将做的是捕获 10 次屏幕,并将“cap%i.png”图像保存在磁盘上。它还将显示为此花费的时间(保存图像不计入该时间,仅计入屏幕截图)。在我的(桌面 Windows 8 - Dell Precision M2800/i7-4810MQ-2.80GHz/Intel HD 4600,这是一台非常糟糕的机器……)机器上,它需要 100 次 1920x1080 捕获在 ~4 秒内,所以大约 20⁄25 fps。
请注意,此代码隐式链接到 WIC (Windows 中包含相当长一段时间的成像库)以保存图像文件(因此您不需要需要安装旧 DirectX SDK 的著名 D3DXSaveSurfaceToFile):
还有我使用的一些宏:
注意:对于 Windows 8+ 客户端,所有这些(WIC 除外)都应放弃,以支持 Desktop Duplication API 。
注意:对于 .NET 用户,这里有一个 C# 版本: DirectN