Jon
Jon

Reputation: 237

How do you convert bytes of bitmap into x, y location of pixels?

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.

Upvotes: 0

Views: 403

Answers (2)

Adrian McCarthy
Adrian McCarthy

Reputation: 48038

The first pixel is typically the bottom-left, though it can be top-left if the height is specified as a negative number.

The scanlines are organized left to right. Note that the scanlines are padded to DWORD boundaries, so the "stride" (distance from one scanline to the next) may be a little more than the actual width of the line.

Upvotes: 2

fbrereto
fbrereto

Reputation: 35945

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?

To answer that question, you could construct an image with unique colors at each corner of the image. From there just query the first pixel.

Upvotes: 0

Related Questions