0

hwndをppmファイルに保存する機能があります。この関数は、msdnの例に触発されています。msdnサンプルと私の関数の両方が機能しますが...問題があります...

しかし、最初に、ここに関数があります。

int CaptureAnImage(HWND hWnd)
{
    HDC hdcWindow;
    HDC hdcMemDC = NULL;
    HBITMAP hbmScreen = NULL;
    RECT rc;
    BITMAPINFOHEADER   bi;

    DWORD dwBmpSize;
    HANDLE hDIB;
    char *lpbitmap;
    int w, h;
    FILE *f;

    // Retrieve the handle to a display device context for the client 
    // area of the window. 
    hdcWindow = GetDC(hWnd);

    // Create a compatible DC which is used in a BitBlt from the window DC
    hdcMemDC = CreateCompatibleDC(hdcWindow); 
    if(!hdcMemDC) {
        MessageBox(hWnd, "CreateCompatibleDC has failed","Failed", MB_OK);
        goto done;
    }

    // Get the client area for size calculation
    GetClientRect(hWnd, &rc);
    w = rc.right - rc.left;
    h=rc.bottom-rc.top;

    // Create a compatible bitmap from the Window DC
    hbmScreen = CreateCompatibleBitmap(hdcWindow, w, h);
    if(!hbmScreen) {
        MessageBox(hWnd, "CreateCompatibleBitmap Failed","Failed", MB_OK);
        goto done;
    }

    // Select the compatible bitmap into the compatible memory DC.
    SelectObject(hdcMemDC,hbmScreen);

    // Bit block transfer into our compatible memory DC.
    if(!BitBlt(hdcMemDC, 
               0,0, 
               w, h, 
               hdcWindow, 
               0,0,
               SRCCOPY)) {
        MessageBox(hWnd, "BitBlt has failed", "Failed", MB_OK);
        goto done;
    }

    bi.biSize = sizeof(BITMAPINFOHEADER);    
    bi.biWidth = w;    
    bi.biHeight = h;  
    bi.biPlanes = 1;    
    bi.biBitCount = 24;    
    bi.biCompression = BI_RGB;    

    bi.biSizeImage = 0;  
    bi.biXPelsPerMeter = 0;    
    bi.biYPelsPerMeter = 0;    
    bi.biClrUsed = 0;    
    bi.biClrImportant = 0;

    dwBmpSize = w*bi.biBitCount*h;

    // Starting with 32-bit Windows, GlobalAlloc and LocalAlloc are implemented as wrapper functions that 
    // call HeapAlloc using a handle to the process's default heap. Therefore, GlobalAlloc and LocalAlloc 

    // have greater overhead than HeapAlloc.
    hDIB = GlobalAlloc(GHND,dwBmpSize); 
    lpbitmap = (char *)GlobalLock(hDIB);    

    // Gets the "bits" from the bitmap and copies them into a buffer 
    // which is pointed to by lpbitmap.
    GetDIBits(hdcWindow, hbmScreen, 0,
        (UINT)h,
        lpbitmap,
        (BITMAPINFO *)&bi, DIB_RGB_COLORS);

    f = fopen("./test.ppm", "wb");
    if (!f) {
        fprintf(stderr, "cannot create ppm file\n");
    goto done;
    }
    fprintf(f, "P6\n%d %d\n255\n", w, h);
    fwrite((LPSTR)lpbitmap, dwBmpSize, 1, f);
    fclose(f);

    //Unlock and Free the DIB from the heap
    GlobalUnlock(hDIB);    
    GlobalFree(hDIB);

    //Clean up
done:
    DeleteObject(hbmScreen);
    DeleteObject(hdcMemDC);
    ReleaseDC(hWnd,hdcWindow);

    return 0;
}

結果の画像は次のとおりです。

http://imageshack.us/photo/my-images/853/test2ne.jpg/

ご覧のとおり、幅のサイズに問題があります。たぶん窓の縁のせい?コード内の場合、「w=rc.right--rc.left;」を変更します。「w=rc.right --rc.left-10;」にすると、より良いです。しかし、なぜ「-10」を付けなければならないのかわかりません...画像の右側にいくつかのピクセルがありません(おそらく10ピクセル?)

http://imageshack.us/photo/my-images/207/test3jq.jpg

そして最後の質問:GetDIBits関数にバイトを逆の順序で配置するように依頼する方法はありますか?ピクセルごとにコピーするのは、CPU時間がかかるため、やりたくありません。(このファイルをディスクに保存しているので、CPU時間は気にしないでください。ただし、この画像をディスクに保存することは目標ではありません。デバッグ目的でのみ実行しています)

助けてくれてありがとう

4

1 に答える 1

1

問題は、DIB 内のイメージ データの各行が DWORD で整列されている (つまり、4 バイトの倍数で整列されている) 必要があることです。

dwBmpSize = w*bi.biBitCount*h;

これは実際には次のようになります。

dwBmpSize = ((w*bi.biBitCount+3)&~3) *h;

PPM ファイルを作成するときは、これを考慮する必要があります。

また、デフォルトの DIB は「ボトムアップ」(行 0 が一番下) であるため、画像は上下逆になっています。「トップダウン」にするには、biHeight フィールドを負の値に設定します。

于 2012-08-16T23:46:17.803 に答える