問題描述
我知道如何使用 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.
我發現這個問題提供了一個示例代碼來做到這一點:
I have found this question that offers a sample code to do that:
我已嘗試包含所需的文件.但是,并非所有這些都可以包含在內(例如 #include
).
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.
我在 Windows 7 Ultimate (x64) 上使用 Visual C++ 2010 Express.
I am using Visual C++ 2010 Express on Windows 7 Ultimate (x64).
另外,這段代碼并不完整,比如Device
標識符是什么?!
Also, this code is not complete, for example what is the Device
identifier?!
推薦答案
以下是一些使用 DirectX 9 捕獲屏幕的示例代碼.您不必安裝任何 SDK(除了 Visual Studio 附帶的標準文件,盡管我沒有測試 VS 2010).
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).
只需創建一個簡單的 Win32 控制臺應用程序,在 stdafx.h 文件中添加以下內容:
Just create a simple Win32 console application, add the following in the stdafx.h file:
這是示例主要實現
這將捕獲 10 倍屏幕,并將cap%i.png"圖像保存在磁盤上.它還會顯示為此花費的時間(保存圖像不計入該時間,只計算屏幕截圖).在我的(桌面 Windows 8 - Dell Precision M2800/i7-4810MQ-2.80GHz/Intel HD 4600,這是一臺非常糟糕的機器......)機器上,它需要在大約 4 秒內捕獲 100 個 1920x1080,所以大約 20/25 fps.
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.
請注意,此代碼隱式鏈接到 WIC(包含一個成像庫使用 Windows 很長一段時間)來保存圖像文件(因此您不需要需要安裝舊 DirectX SDK 的著名 D3DXSaveSurfaceToFile):
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):
還有一些我用過的宏:
注意:對于 Windows 8+ 客戶端,應放棄所有這些(WIC 除外)以支持 桌面復制 API.
Note: for Windows 8+ clients, all these (except WIC) should be dropped in favor of the Desktop Duplication API.
這篇關于使用 DirectX 捕獲屏幕的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!