Win32 window capture with BitBlt not displaying border

◇◆丶佛笑我妖孽 提交于 2019-12-10 10:18:21

问题


I have written some c++ code to capture a window to a .bmp file.

BITMAPFILEHEADER get_bitmap_file_header(int width, int height)
{
 BITMAPFILEHEADER hdr;
 memset(&hdr, 0, sizeof(BITMAPFILEHEADER));
 hdr.bfType  = ((WORD) ('M' << 8) | 'B'); // is always "BM"
 hdr.bfSize  = 0;//sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (width * height * sizeof(int));
 hdr.bfReserved1 = 0;
 hdr.bfReserved2 = 0;
 hdr.bfOffBits = (DWORD)(sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER));

 return hdr;
}

BITMAPINFO get_bitmap_info(int width, int height)
{
 BITMAPINFO bmi;
 memset(&bmi.bmiHeader, 0, sizeof(BITMAPINFOHEADER));
 //initialize bitmap header
 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
 bmi.bmiHeader.biWidth = width;
 bmi.bmiHeader.biHeight = height;
 bmi.bmiHeader.biPlanes = 1;
 bmi.bmiHeader.biBitCount = 4 * 8;
 bmi.bmiHeader.biCompression = BI_RGB;
 bmi.bmiHeader.biSizeImage = width * height * 4;

 return bmi;
}

void get_bitmap_from_window(HWND hWnd, int * imageBuff)
{
 HDC hDC = GetWindowDC(hWnd);
 SIZE size = get_window_size(hWnd);
 HDC hMemDC = CreateCompatibleDC(hDC);
 RECT r;

 HBITMAP hBitmap = CreateCompatibleBitmap(hDC, size.cx, size.cy);
 HBITMAP hOld = (HBITMAP)SelectObject(hMemDC, hBitmap);

 BitBlt(hMemDC, 0, 0, size.cx, size.cy, hDC, 0, 0, SRCCOPY);
    //PrintWindow(hWnd, hMemDC, 0);

 BITMAPINFO bmi = get_bitmap_info(size.cx, size.cy);
 GetDIBits(hMemDC, hBitmap, 0, size.cy, imageBuff, &bmi, DIB_RGB_COLORS);

 SelectObject(hMemDC, hOld);
    DeleteDC(hMemDC);
    ReleaseDC(NULL, hDC);
}

void save_image(HWND hWnd, char * name)
{
 int * buff;
 RECT r;
 SIZE size;

 GetWindowRect(hWnd, &r);
 size.cx = r.right-r.left;
 size.cy = r.bottom-r.top;

 buff = (int*)malloc(size.cx * size.cy * sizeof(int));

 get_bitmap_from_window(hWnd, buff);

 BITMAPINFO bmi = get_bitmap_info(size.cx, size.cy);
 BITMAPFILEHEADER hdr = get_bitmap_file_header(size.cx, size.cy);

 FILE * fout = fopen(name, "w");
 fwrite(&hdr, 1, sizeof(BITMAPFILEHEADER), fout);
 fwrite(&bmi.bmiHeader, 1, sizeof(BITMAPINFOHEADER), fout);
 fwrite(buff, 1, size.cx * size.cy * sizeof(int), fout);

 fflush(fout);
 fclose(fout);
 free(buff);
}

It works find under XP, but under Vista the border of the window is transparent.

Using PrintWindow solves the problem, but is unacceptable for performance reasons.

Is there a performant code change, or a setting that can be changed to make the border non-transparent?


回答1:


Turns out this was due to the WS_EX_LAYERED attribute being set on the window.

See http://www.eggheadcafe.com/software/aspnet/31543575/dwm-composition--get-par.aspx for a discussion of related issues.

Oddly using PrintWindow once and BitBlt afterwards solves the problem...




回答2:


Use below instead

BitBlt(hMemDC, 0, 0, size.cx, size.cy, hDC, 0, 0, SRCCOPY | CAPTUREBLT);


来源:https://stackoverflow.com/questions/2474538/win32-window-capture-with-bitblt-not-displaying-border

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