如何使用DirectX从后台缓冲区获取桌面的屏幕截图 [英] How to get the screenshot of the desktop from back buffer with DirectX

查看:400
本文介绍了如何使用DirectX从后台缓冲区获取桌面的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问了如何在DirectX中拍摄屏幕截图并且回答了使用GetFrontBufferData()。但是,在 MSDN

I asked how to take the screen shot in DirectX and "Use GetFrontBufferData()" was answered. However, "This function is very slow, by design, and should not be used in any performance-critical path" was described at MSDN.

当我问其他人该措施时,回答将后缓冲区的数据传输到系统内存。但是,我找不到具体的方法。

When I asked other persons the measure, "Transfer the data of a back buffer to the system memory" was answered. But, I was not able to find a concrete method.

这是失败的一个例子。请让我知道正确的代码。

This is an example of failure. Please let me know the right code.

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

void main()
{
    CoInitialize(NULL);

    HWND hwnd = GetDesktopWindow();

    LPDIRECT3D9 d3d9;
    D3DDISPLAYMODE ddm;

    d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
    d3d9->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&ddm);

    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory(&d3dpp,sizeof(D3DPRESENT_PARAMETERS));
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
    d3dpp.BackBufferCount = 1;
    d3dpp.BackBufferWidth = ddm.Width;
    d3dpp.BackBufferHeight = ddm.Height;
    d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
    d3dpp.MultiSampleQuality = 0;
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
    d3dpp.hDeviceWindow = hwnd;
    d3dpp.Flags = D3DPRESENTFLAG_VIDEO;
    d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
    d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;

    LPDIRECT3DDEVICE9 dev;
    d3d9->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING ,&d3dpp, &dev);

    /* Deprecation
    IDirect3DSurface9* surface = NULL;
    dev->CreateOffscreenPlainSurface(ddm.Width, ddm.Height, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &surface, NULL);
    dev->GetFrontBufferData(0, surface);
    D3DXSaveSurfaceToFile(L"D:\\test.bmp", D3DXIFF_BMP, surface, NULL, NULL);
    */

    // Transfer the data of a back buffer to the system memory
    IDirect3DSurface9* surface = NULL;
    dev->CreateOffscreenPlainSurface(ddm.Width, ddm.Height, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &surface, NULL);
    LPDIRECT3DSURFACE9 back = NULL;
    dev->SetRenderTarget(0, surface);
    dev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &back);
    dev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,127), 1.0, 0 );
    dev->BeginScene();
    dev->StretchRect(surface, NULL, back, NULL, D3DTEXF_NONE);
    dev->EndScene();
    dev->Present(0,0,0,0);
    D3DXSaveSurfaceToFile(L"D:\\test.bmp", D3DXIFF_BMP, back, NULL, NULL);


    if(back) back->Release();

    dev->Release();
    d3d9->Release();

    CoUninitialize();
}


推荐答案

您无法获取数据从后缓冲区中,函数GetBackBuffer仅检索后缓冲区的指针,而不是其中的数据。

You can not get the data from back-buffer, the function GetBackBuffer only retrieve a pointer of the back buffer, not the data in it.


  • 前端缓冲区。
    图形适配器转换并在监视器上显示的内存矩形。在Direct3D中,
    应用程序永远不会直接写入前端缓冲区。

  • 后缓冲区。应用程序可以直接写入的
    内存矩形。
    后缓冲区永远不会直接显示在监视器上。

这意味着您在桌面上看到的内容位于前缓冲区中

that means what you saw on the desktop exists in front buffer.

这篇关于如何使用DirectX从后台缓冲区获取桌面的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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