# Thread: Create 24 bits bitmap in memory

1. ## Create 24 bits bitmap in memory

Hello. I'm trying to generate a 24-bits bitmap and loop through each pixel, calulate the new pixel value. I read somewhere that when the scan line byte count is odd, it must be padded with zeros. I'm trying to implement this correctly but i don't seem to manage.
rcFill is the rectangle I want to fill with the generated bitmap. When I make the right value 400, it works. With 401, it also works. But with 402 the bitmap looks distorted as if it were on a grayscale tv screen showing the contents of a broken VHS tape. Can anyone see what's wrong in my algorithm? I previously used 32 bits bitmaps, so I didn't have to pad the scan lines, and that worked fine, so the problem isn't in my calcuations.
Below is my code and two screenshots respectively of the result when specifying 401 as the right value of the RECT (so cx is 361), and the result with 402.

Code:
```static RECT rcFill = { 40, 40, 402, 400 };    //rectangle we want to fill with a fancy gradient

// ...

int cx = (rcFill.right-rcFill.left);       //width of the rectangle
int cy = (rcFill.bottom-rcFill.top);   //height of the rectangle
int cScan = cx * 3;                     //number of bytes in a scan line
bool fOddScanWidth = (cScan&#37;2);
if(fOddScanWidth) cScan++;     //if cScan is odd add an extra byte
int bytes = cScan * cy;         //allocate memory block for pixel data:
unsigned char* puc = new unsigned char[bytes], *p = puc;  //p is the pointer we use for our loop
memset(puc, 0, bytes);
for(int y=0; y<cy; ++y)
{
for(int x=0; x<cx; ++x)
{
unsigned char R = 0, G = 0, B = 0;

// calculations...

*p = B;
p[1] = G;
p[2] = R;

p += 3;
}
if(fOddScanWidth) ++p;
}

BITMAPINFO bmi =
{
{
cx, -cy,
1, 24,
},
};
SetDIBits(NULL, hMemBmp, 0, rcFill.bottom - rcFill.top, puc, &bmi, DIB_RGB_COLORS);
delete [] puc;```

2. I thought each bitmap row was supposed to be a multiple of 4 bytes long, not 2.

3. oops, gotta try that

4. Thanks Salem, it works now for every value. What a simple answer to such a large post
Gotta develop my program further.

5. My
new
Code:
```            int cx = (rcFill.right-rcFill.left);
int cy = (rcFill.bottom-rcFill.top);
int cScan = cx * 3;
int bytes = cScan * cy;
unsigned char* puc = new unsigned char[bytes], *p = puc;
memset(puc, 0, bytes);
for(int y=0; y<cy; ++y)
{
for(int x=0; x<cx; ++x)
{
///...
p += 3;
}