How do you convert bytes of bitmap into x, y location of pixels?
Posted
by Jon
on Stack Overflow
See other posts from Stack Overflow
or by Jon
Published on 2010-05-25T17:59:07Z
Indexed on
2010/05/25
18:01 UTC
Read the original article
Hit count: 205
I have a win32 program that creates a bitmap screenshot. I am trying to figure out the x and y coordinates of the bmBits. Below is the code I have so far:
UINT32 nScreenX = GetSystemMetrics(SM_CXSCREEN);
UINT32 nScreenY = GetSystemMetrics(SM_CYSCREEN);
HDC hdc = GetDC(NULL);
HDC hdcScreen = CreateCompatibleDC(hdc);
HBITMAP hbmpScreen = CreateDIBSection( hdcDesk, ( BITMAPINFO* )&bitmapInfo.bmiHeader,DIB_RGB_COLORS, &bitmapDataPtr, NULL, 0 );
SelectObject(hdcScreen, hbmpScreen);
BitBlt(hdcScreen, 0, 0, nScreenX , nScreenY , hdc, 0, 0, SRCCOPY);
ReleaseDC(NULL, hdc);
BITMAP bmpScreen;
GetObject(hbmpScreen, sizeof(bmpScreen), &bmpScreen);
DWORD *pScreenPixels = (DWORD*)bmpScreen.bmBits,
UINT32 x = 0;
UINT32 y = 0;
UINT32 nCntPixels = nScreenX * nScreenY;
for(int n = 0; n < nCntPixels; n++)
{
x = n % nScreenX;
y = n / nScreenX;
//do stuff with the x and y vals
}
The code seem correct to me but, when I use this code the x and y values appear to be off. Where does the first pixel of bmBits start? When x and y are both 0. Is that the top left, bottom left, bottom right or top right?
Thanks.
© Stack Overflow or respective owner