本文介绍了使用DirectX捕获屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道如何使用GDI捕获屏幕,但是它很慢(它几乎不捕获10 fps)
I know how to use GDI to capture screen, however it is very slow (it barely captures 10 fps)
我读过DirectX提供最好的速度。但在我开始学习DirectX之前,我想测试一个示例,看看它是否真的那么快。
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.
我发现这个(Windows中包含的成像库已有很长时间了)保存图像(因此,需要安装需要安装旧DirectX SDK的着名的D3DXSaveSurfaceToFile):
To compile this, you will also need WIC (an imaging library included with Windows for quite a time now) to save an image (so you don't need the famous D3DXSaveSurfaceToFile that require old DirectX SDKs to be installed):
HRESULT SavePixelsToFile32bppPBGRA(UINT width, UINT height, UINT stride, LPBYTE pixels, LPWSTR filePath, const GUID &format)
{
if (!filePath || !pixels)
return E_INVALIDARG;
HRESULT hr = S_OK;
IWICImagingFactory *factory = nullptr;
IWICBitmapEncoder *encoder = nullptr;
IWICBitmapFrameEncode *frame = nullptr;
IWICStream *stream = nullptr;
GUID pf = GUID_WICPixelFormat32bppPBGRA;
BOOL coInit = CoInitialize(nullptr);
HRCHECK(CoCreateInstance(CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&factory)));
HRCHECK(factory->CreateStream(&stream));
HRCHECK(stream->InitializeFromFilename(filePath, GENERIC_WRITE));
HRCHECK(factory->CreateEncoder(format, nullptr, &encoder));
HRCHECK(encoder->Initialize(stream, WICBitmapEncoderNoCache));
HRCHECK(encoder->CreateNewFrame(&frame, nullptr)); // we don't use options here
HRCHECK(frame->Initialize(nullptr)); // we dont' use any options here
HRCHECK(frame->SetSize(width, height));
HRCHECK(frame->SetPixelFormat(&pf));
HRCHECK(frame->WritePixels(height, stride, stride * height, pixels));
HRCHECK(frame->Commit());
HRCHECK(encoder->Commit());
cleanup:
RELEASE(stream);
RELEASE(frame);
RELEASE(encoder);
RELEASE(factory);
if (coInit) CoUninitialize();
return hr;
}
我使用的一些宏:
#define WIDEN2(x) L ## x
#define WIDEN(x) WIDEN2(x)
#define __WFILE__ WIDEN(__FILE__)
#define HRCHECK(__expr) {hr=(__expr);if(FAILED(hr)){wprintf(L"FAILURE 0x%08X (%i)\n\tline: %u file: '%s'\n\texpr: '" WIDEN(#__expr) L"'\n",hr, hr, __LINE__,__WFILE__);goto cleanup;}}
#define RELEASE(__p) {if(__p!=nullptr){__p->Release();__p=nullptr;}}
:对于Windows 8+客户端,应该删除所有这些(WIC除外)以支持。
这篇关于使用DirectX捕获屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!