截屏 C++ directx 得到黑色图像

发布于 2024-11-24 13:59:11 字数 1548 浏览 1 评论 0原文

我正在尝试使用 directx 进行屏幕截图。我尝试了几页代码来解释如何操作,但总是得到黑屏截图。如果有人能帮助我,我将不胜感激。

我的代码是:

int _tmain(int argc, _TCHAR* argv[])
{
    IDirect3DDevice9* g_pd3dDevice;
    //SaveScreenShot("test.bmp",D3DXIFF_BMP);   
    IDirect3DSurface9* pSurface=NULL;
    //IDirect3DSurface9* pSource=NULL;
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory(&d3dpp, sizeof(d3dpp));
    d3dpp.Windowed = FALSE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
    d3dpp.BackBufferCount = 1;
    d3dpp.BackBufferWidth = 1280;
    d3dpp.BackBufferHeight = 1024;
    d3dpp.hDeviceWindow = GetDesktopWindow();
    d3dpp.FullScreen_RefreshRateInHz=0;
    d3dpp.MultiSampleQuality=0;
    IDirect3D9* direct=Direct3DCreate9(D3D9b_SDK_VERSION);
    direct->CreateDevice(D3DADAPTER_DEFAULT,
                         D3DDEVTYPE_HAL,
                         GetDesktopWindow(),
                         D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                         &d3dpp,&g_pd3dDevice);


    //g_pd3dDevice->GetRenderTarget(0,&pSource);
    g_pd3dDevice->CreateOffscreenPlainSurface(1280, 1024,
        D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &pSurface, NULL);
    g_pd3dDevice->GetFrontBufferData(0, pSurface);
    /*g_pd3dDevice->GetRenderTargetData(pSource,pSurface);
    g_pd3dDevice->GetRenderTargetData(pSource,pSurface);*/
    D3DXSaveSurfaceToFile("Desktop.jpg",D3DXIFF_JPG,pSurface,NULL,NULL);
    pSurface->Release();
    //pSource->Release();
    return 0;
}

I'm trying to take a screen capture with directx. I tried code from several pages that explains how but always get a black screen shot. If someone can help me I will be grateful.

my code is:

int _tmain(int argc, _TCHAR* argv[])
{
    IDirect3DDevice9* g_pd3dDevice;
    //SaveScreenShot("test.bmp",D3DXIFF_BMP);   
    IDirect3DSurface9* pSurface=NULL;
    //IDirect3DSurface9* pSource=NULL;
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory(&d3dpp, sizeof(d3dpp));
    d3dpp.Windowed = FALSE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
    d3dpp.BackBufferCount = 1;
    d3dpp.BackBufferWidth = 1280;
    d3dpp.BackBufferHeight = 1024;
    d3dpp.hDeviceWindow = GetDesktopWindow();
    d3dpp.FullScreen_RefreshRateInHz=0;
    d3dpp.MultiSampleQuality=0;
    IDirect3D9* direct=Direct3DCreate9(D3D9b_SDK_VERSION);
    direct->CreateDevice(D3DADAPTER_DEFAULT,
                         D3DDEVTYPE_HAL,
                         GetDesktopWindow(),
                         D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                         &d3dpp,&g_pd3dDevice);


    //g_pd3dDevice->GetRenderTarget(0,&pSource);
    g_pd3dDevice->CreateOffscreenPlainSurface(1280, 1024,
        D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &pSurface, NULL);
    g_pd3dDevice->GetFrontBufferData(0, pSurface);
    /*g_pd3dDevice->GetRenderTargetData(pSource,pSurface);
    g_pd3dDevice->GetRenderTargetData(pSource,pSurface);*/
    D3DXSaveSurfaceToFile("Desktop.jpg",D3DXIFF_JPG,pSurface,NULL,NULL);
    pSurface->Release();
    //pSource->Release();
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嗫嚅 2024-12-01 13:59:11

尝试添加一些错误处理,也许您会知道它失败的原因。

Try adding some error handling and perhaps you will get to know why it fails.

一绘本一梦想 2024-12-01 13:59:11

我认为代码显示了黑色屏幕截图,因为您正在尝试保存前端缓冲区,而在您调用 D3DXSaveSurfaceToFile() 时该缓冲区是空的。您应该首先调用 IDirect3dDevice9::Present(),或者执行以下操作:

  • 在设备的后台缓冲区上绘制您想要的任何内容
  • 获取后台缓冲区的 DC 句柄(源)
  • 通过调用 BitBlt 使用 GDI,将源 DC 的内容复制到目标 DC,然后您可以将其写入文件、打印等。Direct3D

不会提供您在屏幕上看到的内容(至少根据我的经验!),只会提供窗口客户区的内容哪个它被创建了。

I think that the code shows a black screenshot because you are trying to save the front buffer, which at the time you call D3DXSaveSurfaceToFile() is empty. You should either call IDirect3dDevice9::Present() first, or do the following:

  • Draw whatever you want on the device's back buffer
  • Get the back buffer's DC handle (source)
  • Use GDI by calling BitBlt, to copy the contents of the source DC to a destination DC, which you can then write to a file, print etc.

Direct3D will not give you the contents of what you see on the screen (at least to my experience!), only the contents of the client area of the window for which it was created.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文