3
I have a function that captures the monitor image and creates a bitmap file with the result, but I would like you to return a string with the content that the file would have, instead of writing it to disk.
The function:
int CaptureRegion(char *filename, int nLeft, int nTop, int nWidth, int nHeight)
{
HWND hDesktopWnd = GetDesktopWindow(); // Desktop handle
HDC hDesktopDC = GetDC(hDesktopWnd); // Desktop DC
BITMAPINFO bi;
void *pBits = NULL;
ZeroMemory(&bi, sizeof(bi));
bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
bi.bmiHeader.biHeight = nHeight;
bi.bmiHeader.biWidth = nWidth;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 24;
bi.bmiHeader.biCompression = BI_RGB;
// bitmap width should be aligned by DWORD under NT
bi.bmiHeader.biSizeImage = ((nWidth * bi.bmiHeader.biBitCount +31)& ~31) /8 * nHeight;
HDC hBmpFileDC=CreateCompatibleDC(hDesktopDC);
HBITMAP hBmpFileBitmap=CreateDIBSection(hDesktopDC,&bi,DIB_RGB_COLORS,&pBits,NULL,0);
SelectObject(hBmpFileDC, hBmpFileBitmap);
BitBlt(hBmpFileDC, 0, 0, nWidth, nHeight, hDesktopDC, nLeft,nTop, SRCCOPY);
HANDLE hFile=CreateFile(filename,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile!=INVALID_HANDLE_VALUE)
{
DWORD dwRet = 0;
BITMAPFILEHEADER bfh;
ZeroMemory(&bfh, sizeof(bfh));
bfh.bfType = 0x4D42;
bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bfh.bfSize = bi.bmiHeader.biSizeImage + bfh.bfOffBits;
WriteFile(hFile, &bfh, sizeof(bfh), &dwRet, NULL);
WriteFile(hFile, &bi.bmiHeader, sizeof(bi.bmiHeader), &dwRet, NULL);
WriteFile(hFile, pBits, bi.bmiHeader.biSizeImage, &dwRet, NULL);
CloseHandle(hFile);
} else return 0;
DeleteDC(hBmpFileDC);
DeleteObject(hBmpFileBitmap);
ReleaseDC(hDesktopWnd,hDesktopDC);
return 1;
}
From what I researched, I have to use the function "Getdibits", but I do not know how to proceed.
Why do you want to get string from a bit map ? What is the utility?
– M8n
I’m using a library in Lua(luapower/bitmap) that loads the bitmap by the file, but the way I’m using it (taking printscreen every second), I don’t like to keep writing the file on the disk over and over again, since I only use it to look for a pixel and then erase it, to search the next image.
– Gabriel Sales
If I concatenated the content of bfh, bi.bmiHeader and pBits, it would have the content that the file would have?
– Gabriel Sales
To you that creating a buffer is not a string.
– M8n
And how do I do that?
– Gabriel Sales
I don’t know moon I’ll just create the buffer, and DI of
GetDIBits
means Device-Independent(Standalone Device) if you are going to use bitmap on the same device you do not need toGetDIBits
you will need toGetBitmapBits
.– M8n
If you want the bits to search for a pixel do not need all the "formality" of the bitmap format, take only the pixels of the image(
pbits
) and return.– Paulo Marcio
But I need it to be bitmap, for use in Lua, I need it to have the content that the file would have.
– Gabriel Sales