Rick
Rick

Reputation: 107

How do I get the byte representation of a BITMAPFILEHEADER and BITMAPINFOHEADER?

I'm trying to take a screenshot of my computer screen using code, and then store the BYTEs of that image into an array rather than save the image right away using the following function:

void WINAPI CaptureScreenIntoByteArray(
   BYTE*& screen_bytes,
   DWORD& screen_bytes_size)
{
   BITMAPFILEHEADER bfHeader;
   BITMAPINFOHEADER biHeader;
   BITMAPINFO bInfo;
   HGDIOBJ hTempBitmap;
   HBITMAP hBitmap;
   BITMAP bAllDesktops;
   HDC hDC, hMemDC;
   LONG lWidth, lHeight;
   BYTE* sb = NULL;

   ZeroMemory(&bfHeader, sizeof(BITMAPFILEHEADER));
   ZeroMemory(&biHeader, sizeof(BITMAPFILEHEADER));
   ZeroMemory(&bInfo, sizeof(BITMAPINFO));
   ZeroMemory(&bAllDesktops, sizeof(BITMAP));

   hDC = GetDC(NULL);
   hTempBitmap = GetCurrentObject(hDC, OBJ_BITMAP);
   GetObjectW(hTempBitmap, sizeof(BITMAP), &bAllDesktops);

   lWidth = bAllDesktops.bmWidth;
   lHeight = bAllDesktops.bmHeight;

   DeleteObject(hTempBitmap);

   bfHeader.bfType = (WORD)('B' | ('M' << 8));
   bfHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

   biHeader.biSize = sizeof(BITMAPINFOHEADER);
   biHeader.biBitCount = 24;
   biHeader.biCompression = BI_RGB;
   biHeader.biPlanes = 1;
   biHeader.biWidth = lWidth;
   biHeader.biHeight = lHeight;

   bInfo.bmiHeader = biHeader;

   screen_bytes_size = (((24 * lWidth + 31) & ~31) / 8) * lHeight;

   hMemDC = CreateCompatibleDC(hDC);
   hBitmap = CreateDIBSection(hDC, &bInfo, DIB_RGB_COLORS, (VOID**)&sb, NULL, 0);
   SelectObject(hMemDC, hBitmap);

   int x = GetSystemMetrics(SM_XVIRTUALSCREEN);
   int y = GetSystemMetrics(SM_YVIRTUALSCREEN);
   BitBlt(hMemDC, 0, 0, lWidth, lHeight, hDC, x, y, SRCCOPY);

   // Need to also copy bfHeader & biHeader bytes somehow...
   screen_bytes = new BYTE[screen_bytes_size];
   std::copy(sb, sb + screen_bytes_size, screen_bytes);

   DeleteDC(hMemDC);
   ReleaseDC(NULL, hDC);
   DeleteObject(hBitmap);
}

My specific issue is that I need to have the bfHeader and biHeader saved into the byte array also (so that later on I can save the image as a file)... So this is the problem area...

// Need to also copy bfHeader & biHeader bytes somehow...
screen_bytes = new BYTE[screen_bytes_size];
std::copy(sb, sb + screen_bytes_size, screen_bytes);

How can I also copy the bfHeader and biHeader bytes into the array?

If needed, this is how you call the function...

BYTE* screen_bytes = NULL;
DWORD screen_bytes_size = 0;
CaptureScreenIntoByteArray(screen_bytes, screen_bytes_size);

Upvotes: 1

Views: 496

Answers (1)

Jonathan Potter
Jonathan Potter

Reputation: 37202

You need to make the array big enough for the two structures as well.

screen_bytes = new BYTE[sizeof(BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER) + screen_bytes_size];

Then you can copy the two structures to the front of the array:

std::copy(&bfHeader, &bfHeader + 1, (BITMAPFILEHEADER*)screen_bytes);
std::copy(&biHeader, &biHeader + 1, (BITMAPINFOHEADER*)(screen_bytes + sizeof(BITMAPFILEHEADER)));

And copy the image data after the two structures.

std::copy(sb, sb + screen_bytes_size, screen_bytes + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER));

Upvotes: 3

Related Questions