【问题标题】:Capture screenshot of OpenGL/Direct3D 3rd party application捕获 OpenGL/Direct3D 3rd 方应用程序的屏幕截图
【发布时间】:2012-09-06 06:45:51
【问题描述】:

我想在我的 VS2008、MFC、C++ 项目中捕获窗口的内容(客户区)。我尝试使用此处描述的 PrintWindow 技术: How to get screenshot of a window as bitmap object in C++?

我还尝试使用以下代码对窗口的内容进行 blitting:

void captureWindow(int winId)
{
HDC handle(::GetDC(HWND(winId)));
CDC sourceContext;
CBitmap bm;
CDC destContext;

if( !sourceContext.Attach(handle) )
{
    printf("Failed to attach to window\n");
    goto cleanup;
}

RECT winRect;
sourceContext.GetWindow()->GetWindowRect(&winRect);
int width = winRect.right-winRect.left;
int height = winRect.bottom-winRect.top;

destContext.CreateCompatibleDC( &sourceContext );

if(!bm.CreateCompatibleBitmap(&sourceContext, width, height)) {
    printf("Failed to create bm\n");
    goto cleanup;
}

{
    //show a message in the window to enable us to visually confirm we got the right window
    CRect rcText( 0, 0, 0 ,0 );
    CPen pen(PS_SOLID, 5, 0x00ffff);
    sourceContext.SelectObject(&pen);
    const char *msg = "Window Captured!";
    sourceContext.DrawText( msg, &rcText, DT_CALCRECT );
    sourceContext.DrawText( msg, &rcText, DT_CENTER );

    HGDIOBJ hOldDest = destContext.SelectObject(bm);
    if( hOldDest==NULL )
    {
        printf("SelectObject failed with error %d\n", GetLastError());
        goto cleanup;
    }

    if ( !destContext.BitBlt( 0, 0, width, height, &sourceContext, 0, 0, SRCCOPY ) ){
        printf("Failed to blit\n");
        goto cleanup;
    }

    //assume this function saves the bitmap to a file
    saveImage(bm, "capture.bmp");

    destContext.SelectObject(hOldDest);
}
cleanup:
    destContext.DeleteDC();
    sourceContext.Detach();
    ::ReleaseDC(0, handle);
}

该代码适用于大多数应用程序。然而,我需要捕获屏幕截图的特定应用程序有一个我认为是使用 OpenGl 或 Direct3D 呈现的窗口。这两种方法都可以很好地捕获应用程序的大部分内容,但“3d”区域会变黑或出现乱码。

我无权访问应用程序代码,因此无法以任何方式更改它。

有什么办法可以捕捉到所有的内容,包括“3d”窗口?

【问题讨论】:

  • 我自己也在看同样的问题。如果您确实找到了,请发布解决方案。
  • 不幸的是我仍然无法解决这个问题。它仍然是我们系统中的一个错误:-(

标签: visual-c++ mfc


【解决方案1】:

您的 3D 区域中的数据是由图形适配器在图形漏斗下方生成的,您的应用程序可能无法从渲染上下文中读取字节数据。在 OpenGL 中,您可以使用 glReadPixels() 将该数据从漏斗中拉回您的应用程序内存中。用法见这里: http://www.opengl.org/sdk/docs/man/xhtml/glReadPixels.xml

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多