2

我有一个函数,它本质上是截屏并将指向它的指针保存为结构。我想对从文件加载的位图使用相同的结构。

typedef struct _BITMAPCAPTURE {
HBITMAP hbm;
LPDWORD pixels;
INT     width;
INT     height;
} BITMAPCAPTURE;

BOOL CaptureScreen(BITMAPCAPTURE* bmpCapture)
{
BOOL bResult = FALSE;
if(!bmpCapture)
    return bResult;

ZeroMemory(bmpCapture, sizeof(BITMAPCAPTURE));

HDC hdcScreen  = GetDC(NULL);
HDC hdcCapture = CreateCompatibleDC(NULL);
int nWidth     = GetSystemMetrics(SM_CXVIRTUALSCREEN),
    nHeight    = GetSystemMetrics(SM_CYVIRTUALSCREEN);

// Bitmap is stored top down as BGRA,BGRA,BGRA when used as
// DWORDs endianess would change it to ARGB.. windows COLORREF is ABGR
LPBYTE lpCapture;
BITMAPINFO bmiCapture = { {
    sizeof(BITMAPINFOHEADER), nWidth, -nHeight, 1, 32, BI_RGB, 0, 0, 0, 0, 0,
} };

bmpCapture->hbm = CreateDIBSection(hdcScreen, &bmiCapture,
    DIB_RGB_COLORS, (LPVOID *)&lpCapture, NULL, 0);
if(bmpCapture->hbm){
    HBITMAP hbmOld = (HBITMAP)SelectObject(hdcCapture, bmpCapture->hbm);
    BitBlt(hdcCapture, 0, 0, nWidth, nHeight, hdcScreen, 0, 0, SRCCOPY);
    SelectObject(hdcCapture, hbmOld);
    bmpCapture->pixels = (LPDWORD)lpCapture;
    bmpCapture->width  = nWidth;
    bmpCapture->height = nHeight;
    bResult = TRUE;
}

DeleteDC(hdcCapture);
DeleteDC(hdcScreen);
return bResult;
}

位图的句柄以及宽度和高度都很容易获得,但我不确定如何获得像素。

4

0 回答 0