qt使用dxgi截屏保存
时间: 2024-01-02 13:03:11 浏览: 190
python调用DXGI实时快速截屏,是python截屏的最快版了
以下是使用Qt和DXGI进行屏幕截图并保存的示例代码:
```c++
#include <QCoreApplication>
#include <QImage>
#include <QFile>
#include <QDebug>
#include <d3d11.h>
#include <dxgi1_2.h>
// Helper function to check HRESULTs
#define CHECK_HR(hr, msg) \
if (FAILED(hr)) { \
qDebug() << msg << "failed with HRESULT" << hr; \
return false; \
}
bool saveScreenshot(const QString& filePath)
{
HRESULT hr = S_OK;
// Create DXGI factory
IDXGIFactory1* pFactory = nullptr;
hr = CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&pFactory);
CHECK_HR(hr, "Failed to create DXGI factory");
// Get primary adapter
IDXGIAdapter1* pAdapter = nullptr;
hr = pFactory->EnumAdapters1(0, &pAdapter);
CHECK_HR(hr, "Failed to get primary adapter");
// Create D3D device and swap chain
D3D_FEATURE_LEVEL featureLevel = D3D_FEATURE_LEVEL_11_0;
D3D_FEATURE_LEVEL featureLevels[] = { featureLevel };
DXGI_SWAP_CHAIN_DESC swapChainDesc = { 0 };
swapChainDesc.BufferCount = 1;
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.OutputWindow = GetDesktopWindow();
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.Windowed = TRUE;
ID3D11Device* pDevice = nullptr;
ID3D11DeviceContext* pContext = nullptr;
IDXGISwapChain* pSwapChain = nullptr;
hr = D3D11CreateDeviceAndSwapChain(pAdapter, D3D_DRIVER_TYPE_UNKNOWN, NULL, 0, featureLevels, 1,
D3D11_SDK_VERSION, &swapChainDesc, &pSwapChain, &pDevice, &featureLevel, &pContext);
CHECK_HR(hr, "Failed to create D3D device and swap chain");
// Get back buffer texture
ID3D11Texture2D* pBackBuffer = nullptr;
hr = pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
CHECK_HR(hr, "Failed to get back buffer texture");
// Create a CPU-readable texture
D3D11_TEXTURE2D_DESC textureDesc = { 0 };
textureDesc.Width = GetSystemMetrics(SM_CXSCREEN);
textureDesc.Height = GetSystemMetrics(SM_CYSCREEN);
textureDesc.MipLevels = 1;
textureDesc.ArraySize = 1;
textureDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
textureDesc.SampleDesc.Count = 1;
textureDesc.Usage = D3D11_USAGE_STAGING;
textureDesc.BindFlags = 0;
textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
ID3D11Texture2D* pTexture = nullptr;
hr = pDevice->CreateTexture2D(&textureDesc, NULL, &pTexture);
CHECK_HR(hr, "Failed to create CPU-readable texture");
// Copy back buffer to CPU-readable texture
pContext->CopyResource(pTexture, pBackBuffer);
// Read texture data
D3D11_MAPPED_SUBRESOURCE mappedResource;
hr = pContext->Map(pTexture, 0, D3D11_MAP_READ, 0, &mappedResource);
CHECK_HR(hr, "Failed to map texture");
QImage image(mappedResource.pData, textureDesc.Width, textureDesc.Height, QImage::Format_RGB32);
pContext->Unmap(pTexture, 0);
// Save image to file
if (!image.save(filePath)) {
qDebug() << "Failed to save screenshot to" << filePath;
return false;
}
// Cleanup
pContext->Release();
pDevice->Release();
pSwapChain->Release();
pAdapter->Release();
pFactory->Release();
pTexture->Release();
pBackBuffer->Release();
return true;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
if (saveScreenshot("screenshot.png")) {
qDebug() << "Screenshot saved to screenshot.png";
}
return a.exec();
}
```
该代码使用DXGI和D3D11 API创建设备和交换链,然后获取主适配器和后缓冲器纹理。然后,它创建一个CPU可读的纹理,将后缓冲器纹理复制到该纹理中,然后将其映射到内存中以获取图像数据。最后,它使用Qt的QImage类将图像数据保存到文件中。请注意,此代码需要使用Windows SDK和DirectX SDK来构建。
阅读全文