捕获OpenGL/Direct3D第三方应用程序的屏幕截图

3
我希望在我的VS2008,MFC,C ++项目中捕获窗口(客户区)的内容。 我尝试使用此处描述的PrintWindow技术: 如何在C ++中获取窗口的屏幕截图作为位图对象? 我还尝试使用以下代码复制窗口的内容:
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”窗口?

如果您找到了解决方案,请发布出来。我也在看同样的问题。 - Aleksandr Makov
很遗憾,我仍然无法解决这个问题。它仍然是我们系统中的一个错误 :-( - Vanvid
1个回答

0

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接