所以,馬上去金屬。我正在編寫一個C++程序,其目的是將屏幕作爲輸入圖像,並使用對象檢測器(來自dlib)處理它,但我有麻煩了解如何獲得指向swapChain和上下文/內容的指針屏幕。是否可以使用DirectX工具包中的screengrab進行桌面/屏幕截圖捕獲?
從ScreenGrab (DirectX Tool Kit)下面的代碼似乎能夠做到這一點。問題是我不知道如何讓它起作用。我在Visual Studio 2017年使用的NuGet獲得庫(稱爲directxtk_uwp,還有哪些我沒有使用一個叫directxtk_desktop_2015)和我得到以下六大誤區:
- 錯誤C2065 : 'swapChain':未聲明的標識符
- 錯誤C2227:左 ' - >的GetBuffer' 必須指向類/結構/聯合/通用型
- 錯誤C2065:immContext':未聲明的標識符
- 錯誤C2228:左'.Get'必須有class/struct/union
- 錯誤C2653:「DX」:不是類或命名空間名稱
- 錯誤C3861:「ThrowIfFailed」:標識符找不到
從ScreenGrab維基頁面運行插入示例代碼:
#include <ScreenGrab.h>
#include <wrl\client.h>
#include <Wincodec.h>
int main() {
using namespace DirectX;
using namespace Microsoft::WRL;
ComPtr<ID3D11Texture2D> backBuffer;
HRESULT hr = swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D),
reinterpret_cast<LPVOID*>(backBuffer.GetAddressOf()));
if (SUCCEEDED(hr))
{
hr = SaveWICTextureToFile(immContext.Get(), backBuffer.Get(),
GUID_ContainerFormatJpeg, L"SCREENSHOT.JPG");
}
DX::ThrowIfFailed(hr);
return 0;
}
我真的剛剛開始用C++編程,並且來自Java編程,我一直在積極開發一年。因此,保持簡單明瞭,將不勝感激^^
只是要清楚,我想要的東西相當快。我已經獲得了screengrab與GDI合作用下面的代碼:
#include <iostream>
#include <Windows.h>
#include <Wincodec.h>
#include <ctime>
#include <cstring>
#include <atlimage.h>
#include <d3d9.h>
using namespace std;
int main()
{
try
{
int nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
HWND hDesktopWnd = GetDesktopWindow();
HDC hDesktopDC = GetDC(hDesktopWnd);
HDC hCaptureDC = CreateCompatibleDC(hDesktopDC);
for (int i = 0; i < 10;i++) {
clock_t begin = clock();
HBITMAP hCaptureBitmap = CreateCompatibleBitmap(hDesktopDC,
nScreenWidth, nScreenHeight);
SelectObject(hCaptureDC, hCaptureBitmap);
BitBlt(hCaptureDC, 0, 0, nScreenWidth, nScreenHeight,
hDesktopDC, 0, 0, SRCCOPY | CAPTUREBLT);
CImage image;
image.Attach(hCaptureBitmap);
image.Save("test.jpg");
DeleteObject(hCaptureBitmap);
cout << double(clock() - begin)/(clock_t)1000 << endl;
}
ReleaseDC(hDesktopWnd, hDesktopDC);
DeleteDC(hCaptureDC);
IDirect3DSurface9 *surface;
system("pause");
}
catch (exception& e)
{
cout << "\nexception thrown!" << endl;
cout << e.what() << endl;
system("pause");
}
}
其結果如下:
0.091
0.05
0.052
0.06
0.047
0.05
0.057
0.06
0.06
0.051
,正如你所看到的,真的只是得到我的高達1/0.47 ≈每秒21幀。我也看到了this的答案,但又一次,我不知道如何獲得屏幕的上下文和swapChain。 謝謝。
您正在使用的示例代碼避免了假設您正試圖截取您自己的渲染目標紋理(直接創建或從您自己的交換鏈獲得)。這適用於您自己的應用程序,但整個桌面截圖是需要使用DXGI API的另一個問題。你應該看看[這篇文章](https://www.codeproject.com/Articles/5051/Various-methods-for-capturing-the-screen#Capture%20It%20the%20GDI%20way)瞭解一些選項。 –
DirectX工具包[wiki](https://github.com/Microsoft/DirectXTK/wiki)上的示例代碼假定您至少在[tutorials](https://github.com/Microsoft/DirectXTK/wiki/Getting-Started),其中我覆蓋了[ThrowIfFiled](https://github.com/Microsoft/DirectXTK/wiki/ThrowIfFailed)。 –
謝謝,查克!我能夠找到該頁面,因爲您可能在變量名稱中看到,我只是記不起該網站。從來沒有得到DirectX的方式來工作,但那是相當早的,我認爲我現在有一個更好的理解來做到這一點。但是,DirectX9並不像您在關於DirectX11的鏈接問題中所說的那樣過時了嗎?無論如何,您的幫助非常感謝! – EvilFeline