I'm trying to access the pixel colors from a image file but can't seem to get it to work properly, my program is only accessing the pixel colors from my active window. I've been stuck on this for a few days and would really appreciate any help. I'm guessing i haven't set a proper handle to a device context to the image but i don't know how to do this
HWND hDesktopWnd; HDC hDesktopDC; HDC hCaptureDC;
HBITMAP hCaptureBitmap;
BITMAPINFO bmi = {0};
RGBQUAD *pPixels;
LPTSTR szFileName;
BITMAP bm; int nScreenWidth, nScreenHeight;
szFileName = "test.bmp";
nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
hDesktopWnd = GetDesktopWindow();
hDesktopDC = GetDC(hwnd);
hCaptureDC = CreateCompatibleDC(hDesktopDC);
hCaptureBitmap = (HBITMAP)LoadImage( NULL, szFileName, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE );
SelectObject(hCaptureDC, hCaptureBitmap);
BitBlt(hCaptureDC, 0, 0, nScreenWidth, nScreenHeight, hDesktopDC, 0,0, SRCCOPY|CAPTUREBLT);
bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
bmi.bmiHeader.biWidth = nScreenWidth;
bmi.bmiHeader.biHeight = nScreenHeight;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
pPixels = new RGBQUAD[nScreenWidth * nScreenHeight];
::GetDIBits(hCaptureDC,
hCaptureBitmap,
0,
nScreenHeight,
pPixels,
&bmi,
DIB_RGB_COLORS);
for(int i= 0;i< nScreenHeight; i++){
for(int j= 0;j< nScreenWidth; j++){
col.red_palette[i][j] = pPixels[(nScreenWidth * (nScreenHeight-(i+1))) + j].rgbRed;
col.green_palette[i][j] = pPixels[(nScreenWidth * (nScreenHeight-(i+1))) + j].rgbGreen;
col.blue_palette[i][j] = pPixels[(nScreenWidth * (nScreenHeight-(i+1))) + j].rgbBlue;
}
}
delete [] pPixels;
ReleaseDC(hDesktopWnd, hDesktopDC);
DeleteDC(hCaptureDC);
DeleteObject(hCaptureBitmap);