首页 > 解决方案 > 如何将 DirectX 纹理复制到表面?

问题描述

我正在尝试捕获整个桌面屏幕(前缓冲区)并为每一帧添加徽标和标题。

我将徽标(.png 或 .jpeg 文件)加载为 IDirect3DTexture9,并尝试将其添加到 IDirect3DSurface9 图像帧(屏幕截图)。

由于我是 DirecX9 的新手,我不知道如何将徽标(纹理)复制到屏幕截图(表面/缓冲区)。任何帮助,将不胜感激。

(如果有任何其他方法可以在不涉及纹理的情况下为每个框架添加徽标,请告诉我。)

编辑:我使用了下面答案中建议的代码。返回的 hr 结果是一个错误。

IDirect3DSurface9 *pSurface = NULL;
pDevice->GetFrontBufferData(0, pSurface); //my screenshot

LPDIRECT3DTEXTURE9 tex = NULL;  //my logo
//[code to load logo from file here]

IDirect3DSurface9 *pSurf = NULL;
tex->GetSurfaceLevel(0, &pSurf);

hr = pDevice->StretchRect(pSurf, NULL, pSurface, NULL, D3DTEXF_NONE);  //HR GIVES AN ERROR

标签: winapivisual-c++directxdirectx-9

解决方案


您可以使用StretchRect。代码看起来像这样(伪):

ID3DSurface9 *pScreenSurface = ... // your screenshot surface should be created in default pool and the same format as your texture(see bellow), like this:
CreateOffscreenPlainSurface(width, height, D3DFMT_X8B8G8R8, D3DPOOL_DEFAULT, &pScreenSurface, NULL);

// Now get a screenshot by using either GetFrontBufferData, or GetBackBuffer/GetRenderTargetData by supplying pScreenSurface as a parameter (not shown here).

ID3DTexture9 *pTexture = ... // your texture should be loaded in default pool and the same format as your screenshot surface, like this:
D3DXCreateTextureFromFileEx(*ppDevice, L"icon.bmp", 40, 40, D3DX_DEFAULT, 0, 
  D3DFMT_X8B8G8R8, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, 
  NULL, &pTexture);

ID3DSurface9 *pSurf;
pTexture->GetSurfaceLevel(0, &pSurf); // don't forget to Release pSurf after StretchRect

RECT rc = { ... initialize the destination rectangle here };
pDevice->StretchRect(pSurf, NULL, pScreenSurface, &rc);

您需要在目标表面内指定一个目标矩形,您希望在其中复制纹理。


推荐阅读