资源简介

使用DXGI录制屏幕,工程基于VS2015,可以正常保存图片

资源截图

代码片段和文件信息

#include “../stdafx.h“
#include “VideoDxCaptor.h“

#include 
#pragma comment(lib “Dwmapi.lib“)

 VideoDXCaptor::VideoDXCaptor(void)  
 { 
 m_lpDDraw = NULL; 
 m_lpDDSPrime = NULL; 
 m_lpDDSBack = NULL;
 }
VideoDXCaptor::~VideoDXCaptor(void) 

Deinit();
}

BOOL VideoDXCaptor::Init()
{
HMODULE hDll = LoadLibrary(“ddraw.dll“);
if (hDll == NULL)
{
(“无法载入ddraw.dll\n“);
return FALSE;
}

// 载入ddraw的导入函数
PFN_DirectDrawCreate DirectDrawCreateFunc = (PFN_DirectDrawCreate)GetProcAddress(hDll “DirectDrawCreate“);
if (DirectDrawCreateFunc == NULL)
{
(“无法找到访问点:DirectDrawCreate\n“);
return FALSE;
}

HRESULT hr = DirectDrawCreateFunc(NULL &m_lpDDraw NULL);
if (FAILED(hr))
{
(“DirectDrawCreate失败\n“);
return FALSE;
}

hr = m_lpDDraw->SetCooperativeLevel(NULL DDSCL_NORMAL);
if (FAILED(hr))
{
(“SetCooperativeLevel失败\n“);
return FALSE; 
}

DDSURFACEDESC DDSdesc;
ZeroMemory(&DDSdesc sizeof(DDSdesc));
DDSdesc.dwSize         = sizeof(DDSdesc);
DDSdesc.dwFlags        = DDSD_CAPS;
DDSdesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
hr = m_lpDDraw->CreateSurface(&DDSdesc &m_lpDDSPrime NULL);
if (FAILED(hr))
{
(“CreateSurface 主表面失败\n“);
return FALSE; 
}

ZeroMemory(&DDSdesc sizeof(DDSdesc));
DDSdesc.dwSize  = sizeof(DDSdesc);
DDSdesc.dwFlags = DDSD_ALL;
hr = m_lpDDSPrime->GetSurfaceDesc(&DDSdesc);
if (FAILED(hr))
{
(“GetSurfaceDesc失败\n“);
return FALSE;
}

// 备份描述信息
memcpy(&m_DDSdesc &DDSdesc sizeof(DDSdesc));

DDSdesc.dwFlags        = DDSD_CAPS | DDSD_HEIGHT |DDSD_WIDTH; 
DDSdesc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
hr = m_lpDDraw->CreateSurface(&DDSdesc &m_lpDDSBack 0);
if (FAILED(hr))
{
(“CreateSurface 后备表面失败\n“);
return FALSE;
}
return TRUE;
}
VOID VideoDXCaptor::Deinit()
{
if (m_lpDDSBack)
{
m_lpDDSBack->Release();
m_lpDDSBack = NULL;
}

if (m_lpDDSPrime)
{
m_lpDDSPrime->Release();
m_lpDDSPrime = NULL;
}

if (m_lpDDraw)
{
m_lpDDraw->Release();
m_lpDDraw = NULL;
}
}

BOOL VideoDXCaptor::CaptureImage(RECT &rect void *pData INT &nLen)
{
if (m_lpDDSBack == NULL)
{
(“DDraw对象未初始化\n“);
return FALSE;
}

HRESULT hr = m_lpDDSBack->BltFast(rect.left rect.top m_lpDDSPrime &rect DDBLTFAST_NOCOLORKEY | DDBLTFAST_WAIT);
if (FAILED(hr))
{
(“BltFast失败\n“);
return FALSE;
}

DDSURFACEDESC surfDesc;
ZeroMemory(&surfDesc sizeof(surfDesc)); 
surfDesc.dwSize = sizeof(surfDesc);
//hr = lpDDSPrime->Lock(&rect &surfdesc DDLOCK_READONLY | DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR  NULL);
hr = m_lpDDSBack->Lock(&rect &surfDesc DDLOCK_READONLY | DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR  NULL);
if (FAILED(hr))
{
(“Lock失败\n“);
return FALSE;
}

// 这里拷贝的是32位数据,我们只需要24位的RGB数据
//memcpy(pBuf (BYTE*)surfDesc.lpSurface surfDesc.dwWidth * surfDesc.dwHeight * surfDesc.ddpfPixelFormat.dwRGBBitCount / 8);

// 将抓上来的图像转成RGB数据(去掉Alpha值)
if (m_DDSdesc.ddpfPixelFormat.dwRGBBitCount == 32)
{
nLen = (rect.right - rect.left) * (rect.bottom - rect.top) * 3;

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-08-21 11:27  screencapture\
     目录           0  2018-08-21 10:41  screencapture\.vs\
     目录           0  2018-08-21 10:41  screencapture\.vs\capturecreen\
     目录           0  2018-08-21 11:10  screencapture\.vs\capturecreen\v14\
     文件       22528  2018-08-21 11:27  screencapture\.vs\capturecreen\v14\.suo
     目录           0  2018-08-21 10:41  screencapture\Captor\
     文件        3663  2017-04-26 00:57  screencapture\Captor\DXCaptor.cpp
     文件         789  2017-04-26 00:57  screencapture\Captor\DXCaptor.h
     文件        6115  2017-04-26 00:57  screencapture\Captor\DXGICaptor.cpp
     文件        1087  2017-04-26 00:57  screencapture\Captor\DXGICaptor.h
     文件        4566  2017-04-26 00:57  screencapture\Captor\GDICaptor.cpp
     文件         750  2017-04-26 00:57  screencapture\Captor\GDICaptor.h
     文件          40  2018-08-21 11:34  screencapture\README.md
     目录           0  2018-08-21 11:28  screencapture\capturecreen\
     文件        3655  2017-04-26 00:57  screencapture\capturecreen\DXCaptor.cpp
     文件         789  2017-04-26 00:57  screencapture\capturecreen\DXCaptor.h
     文件        4558  2017-04-26 00:57  screencapture\capturecreen\GDICaptor.cpp
     文件         740  2017-04-26 00:57  screencapture\capturecreen\GDICaptor.h
     文件        1710  2017-04-26 00:57  screencapture\capturecreen\ReadMe.txt
     文件        8341  2018-08-21 11:34  screencapture\capturecreen\capturecreen.cpp
     文件        8046  2018-08-21 11:21  screencapture\capturecreen\capturecreen.vcxproj
     文件        1339  2018-08-21 11:21  screencapture\capturecreen\capturecreen.vcxproj.filters
     文件         291  2017-04-26 00:57  screencapture\capturecreen\stdafx.cpp
     文件         305  2017-04-26 00:57  screencapture\capturecreen\stdafx.h
     文件         306  2017-04-26 00:57  screencapture\capturecreen\targetver.h
     文件        1290  2017-04-26 00:57  screencapture\capturecreen.sln

评论

共有 条评论