Problem: can’t get image to the screen… ??
I am looking for a methodology that I can work from the camera and a file that I can modify the bitmap using GetDIBits and SetDIBits and write to the screen.
So far from a file to the screen… not working
HDC hdcScreen;
HDC hdcWindow;
HDC hdcMemDC = NULL;
HBITMAP hbmScreen = NULL;
BITMAP bmpScreen;
BITMAPFILEHEADER bmfHeader;
BITMAPINFOHEADER bi;
BITMAPINFO bif;
// Retrieve the handle to a display device context for the client
// area of the window.
hdcScreen = ::GetDC(NULL);
// hdcWindow = ::GetDC(hWndC);
// Create a compatible DC which is used in a BitBlt from the window DC
hdcMemDC = CreateCompatibleDC(hdcWindow);
HANDLE hFile = ::CreateFile("c:\\captureqwsx.bmp",
GENERIC_READ,
0,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
DWORD nBytesRead = 0;
ReadFile(hFile, (LPSTR)&bmfHeader, sizeof(BITMAPFILEHEADER), &nBytesRead, NULL);
ReadFile(hFile, (LPSTR)&bi, sizeof(BITMAPINFOHEADER), &nBytesRead, NULL);
//HANDLE hDIB = GlobalAlloc(GHND,dwBmpSize);
char *lpbitmap;// = (char *)GlobalLock(hDIB);
DWORD dwBmpSize;// = ((bmpScreen.bmWidth * bi.biBitCount + 31) / 32) * 4 * bmpScreen.bmHeight;
ReadFile(hFile, (LPSTR)lpbitmap, dwBmpSize, &nBytesRead, NULL);
//Close the handle for the file that was created
CloseHandle(hFile);
//CRect rect;
//GetClientRect(&rect);
bif.bmiHeader = bi;
HDC hDC = hdcWindow;
HBITMAP hBitmap;
HDC hMemDC;
hBitmap = CreateCompatibleBitmap(hDC, bi.biWidth, bi.biHeight);
hMemDC = CreateCompatibleDC(hDC);
SetDIBits(hDC, hBitmap, 0, bi.biHeight, lpbitmap, &bif, DIB_RGB_COLORS);
SelectObject(hMemDC, hBitmap);
BitBlt(hDC, 0, 0, bi.biWidth, bi.biHeight, hMemDC, 0, 0, SRCCOPY);
DeleteObject(SelectObject(hMemDC, hBitmap));
DeleteDC(hMemDC);
Here you retrieve the DC for the entire screen. Is that what you really want?
Perhaps you should remove that and uncomment the following line:
According to posted example, you are calling CreateCompatibleDC() with uninitialized ‘hdcWindow’ argument.
After reading BITMAPFILEHEADER and BITMAPINFOHEADER, you should move file pointer to BITMAPFILEHEADER::bfOffBits offest.
Then you should call ReadFile() to read the bitmap itself. Btw, required buffer size is in BITMAPINFOHEADER::biSizeImage.
Since hdcWindow is uninitialized, the hDC remains uninitialized too:
Did you check the return value of SetDIBits()?
Try to modify the following line:
Then, after BitBlt():
I don’t know if this is final solution for your problem, but it’s a start.