2012-09-06 63 views
3

我想在VS2008,MFC,C++項目中捕獲窗口的內容(客戶區)。 How to get screenshot of a window as bitmap object in C++?OpenGL/Direct3D第三方應用截圖截圖

我也曾嘗試使用下面的代碼塊傳輸窗口的內容:

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); 
} 

的代碼工作正常,對於大多數應用我所用這裏所描述的PrintWindow技術嘗試。然而,我需要捕獲屏幕截圖的具體應用程序有一個窗口,我認爲它使用OpenGl或Direct3D呈現。這兩種方法都可以捕捉大部分應用程序,但「3d」區域將保持黑色或亂碼。

我無權訪問應用程序代碼,所以我無法以任何方式更改它。

有什麼方法可以捕獲所有的內容,包括「3d」窗口?

+0

自己看看同樣的問題。請發佈解決方案,如果你找到了。 –

+0

不幸的是我仍然無法解決這個問題。它仍然是我們系統中的一個錯誤:-( – Vanvid

回答

0

3D區域中的數據是由圖形接口下方的圖形適配器生成的,可能無法讓您的應用程序從渲染上下文中讀取字節數據。在OpenGL中,您可以使用glReadPixels()將該數據備份到應用程序內存中。請看這裏使用: http://www.opengl.org/sdk/docs/man/xhtml/glReadPixels.xml