DX9/10/11 Sceen Capture C#

本文关键字:Capture Sceen DX9 | 更新日期: 2023-09-27 18:33:19

我正在阅读这个弧形:http://www.ring3circus.com/gameprogramming/case-study-fraps/我已经意识到,考虑到它的年龄(作者甚至建议不要使用他的代码(,这真的不能满足我的需求(一,它是 C#,二,它是 DX9(。我正在尝试获取 DirectX 游戏窗口的矩形捕获,并将其作为我可以引用的某种流视频保存在内存中。如果不在前台,窗口仍然需要能够被捕获,甚至可见(埋在其他窗口后面(。我已经尝试过那个 C# 库(用 EasyHook 和 SlimDX 制作的库(,但我只设法让演示在 64 位以下的 DirectX 11 游戏中成功运行一次(此后无法重现它(。即便如此,捕获速度也慢得令人瞠目结舌。以及许多其他涵盖覆盖到 DirectX 的内容(我不需要(。我想模仿 fraps 在捕获方面的作用,但我只需要游戏的矩形区域,而不是整个东西,而且我不需要将其写入文件。我是否可以获得一个代码示例,说明如何在 C# 中快速捕获游戏窗口的给定矩形,该矩形适用于 DX9 到 DX11?

DX9/10/11 Sceen Capture C#

这是我针对

同一问题的C++代码。

ID3D11Texture2D* pSurface;
HRESULT hr = m_swapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), reinterpret_cast< void** >( &pSurface ) );
if( pSurface )
{
    const int width = static_cast<int>(m_window->Bounds.Width * m_dpi / 96.0f);
    const int height = static_cast<int>(m_window->Bounds.Height * m_dpi / 96.0f);
    unsigned int size = width * height;
    if( m_captureData )
    {
        freeFramebufferData( m_captureData );
    }
    m_captureData = new unsigned char[ width * height * 4 ];
    ID3D11Texture2D* pNewTexture = NULL;
    D3D11_TEXTURE2D_DESC description;
    pSurface->GetDesc( &description );
    description.BindFlags = 0;
    description.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
    description.Usage = D3D11_USAGE_STAGING;
    HRESULT hr = m_d3dDevice->CreateTexture2D( &description, NULL, &pNewTexture );
    if( pNewTexture )
    {
        m_d3dContext->CopyResource( pNewTexture, pSurface );
        D3D11_MAPPED_SUBRESOURCE resource;
        unsigned int subresource = D3D11CalcSubresource( 0, 0, 0 );
        HRESULT hr = m_d3dContext->Map( pNewTexture, subresource, D3D11_MAP_READ_WRITE, 0, &resource );
        //resource.pData; // TEXTURE DATA IS HERE
        const int pitch = width << 2;
        const unsigned char* source = static_cast< const unsigned char* >( resource.pData );
        unsigned char* dest = m_captureData;
        for( int i = 0; i < height; ++i )
        {
            memcpy( dest, source, width * 4 );
            source += pitch;
            dest += pitch;
        }
        m_captureSize = size;
        m_captureWidth = width;
        m_captureHeight = height;
        return;
    }
    freeFramebufferData( m_captureData );
}

你/可能/能够将其移植到 C#,或者至少将其包装在库中。我问了一个类似的问题,但后来找到了答案:DirectX 11 帧缓冲捕获(C++,没有 Win32 或 D3DX(