使用DirectX进行桌面捕获无法正常工作 [英] The desktop capture with DirectX does not work

查看:114
本文介绍了使用DirectX进行桌面捕获无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于D3DPOOL_SCRATCH处理缓慢,因此我编写了桌面捕获程序以供Internet上的报告参考。但是,结果是黑色的画面。这是控制台程序的结果还是其他原因?

Because processing was slow by D3DPOOL_SCRATCH, I wrote the desktop capture program to reference for the report on the Internet. However, a result is a pitch-black picture. Is this the result of a console program or is there any other cause?

#include <stdio.h>
#include <Windows.h>
#include <d3d9.h>

void main()
{
    CoInitialize(NULL);

    LPDIRECT3D9 d3d9;
    LPDIRECT3DDEVICE9 d3ddev;
    d3d9 = Direct3DCreate9(D3D_SDK_VERSION);

    int ww = GetSystemMetrics(SM_CXSCREEN);
    int wh = GetSystemMetrics(SM_CYSCREEN);

    HWND hwnd = GetDesktopWindow();
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory(&d3dpp, sizeof(d3dpp));
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
    d3dpp.BackBufferCount = 1;
    d3dpp.BackBufferWidth = ww;
    d3dpp.BackBufferHeight = wh;
    d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
    d3dpp.MultiSampleQuality = 0;
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
    d3dpp.hDeviceWindow = hwnd;
    d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
    d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
    d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;

    d3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);

    IDirect3DSurface9* render;
    IDirect3DSurface9* dest;
    d3ddev->CreateOffscreenPlainSurface(ww, wh, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &dest, NULL);
    d3ddev->GetRenderTarget(0, &render);
    d3ddev->GetRenderTargetData(render, dest);

    D3DLOCKED_RECT bits;
    dest->LockRect(&bits, NULL, D3DLOCK_READONLY);

    // If a capture is successful, colors other than black(0x00000000) should enter. 
    for(int i = 0; i < 100; i++){
        printf("%02X ", *((BYTE*)bits.pBits + i));
    }

    dest->UnlockRect();

    render->Release();
    dest->Release();
    d3ddev->Release();
    d3d9->Release();

    CoUninitialize();
}


推荐答案

这与应用程序类型,如果要获取桌面图像的数据,则应使用以下功能

This is nothing related to the application type, if you want to get the data of desktop image, you should use the following function

GetFrontBufferData

因此,

d3ddev->GetRenderTarget(0, &render);
d3ddev->GetRenderTargetData(render, dest);

您应该致电

d3ddev->GetFrontBufferData(0, dest);

这篇关于使用DirectX进行桌面捕获无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆