hwnd to ppm issue

Deadly 提交于 2019-12-08 13:09:50

问题


I have a function which save a hwnd into a ppm file. This function is inspired by a msdn example. Both the msdn sample and my function work but ... I have an issue ...

But first, here is the function.

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

So here is the resulting image:

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

As you can see, there is a problem in the width size. Maybe because of the border of the window ? If in the code, I change "w = rc.right - rc.left;" into "w = rc.right - rc.left - 10;", it's better. But I don't understand why I have to put "-10" and ... some pixel are missing on the right of the picture (maybe 10 pixels ?)

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

And the last question: is there any way to ask to GetDIBits function to put my byte in the inverted order ? I don't wand to do a pixel by pixel copy since it will cost some cpu time. (ok, you may say that since I'm saving this file to disk, then I should not be concerned by cpu time, but my goal is not to save this picture to the disk. I'm doing it for debug purpose only)

thanks in advance for any help


回答1:


Your problem is that each row of image data in a DIB must be DWORD aligned (i.e. aligned on a multiple of 4 bytes).

dwBmpSize = w*bi.biBitCount*h;

This should actually be:

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

You will then have to account for this when writing the PPM file.

Also, the image is upside down because by default DIBs are "bottom-up" (row 0 is at the bottom). To make it "top-down" set the biHeight field to a negative value.



来源:https://stackoverflow.com/questions/11991675/hwnd-to-ppm-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!