我想在我的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”窗口?
答案 0 :(得分:0)
3D区域中的数据由图形漏斗下方的图形适配器生成,可能无法供应用程序从渲染上下文中读取字节数据。在OpenGL中,您可以使用glReadPixels()将数据从漏斗中提取到应用程序内存中。请参阅此处了解用法: http://www.opengl.org/sdk/docs/man/xhtml/glReadPixels.xml