The CreateDIBSection documentation isn't entirely clear, but basically it works like this:
You fill out a BITMAPINFO structure with the desired specs of the bitmap you want. Generally you'd make a 32-bit bitmap with width and height as whatever you want.
The "pitch" of the bitmap is equal to width*bytes_per_pixel, and if necessary is padded up to the next 4 byte boundary (so if width*bytes was 23, the pitch would actually be 24). If you have a 32-bit bitmap, you don't need to worry about that padding.
You give CreateDIBSection that BITMAPINFO struct you filled out, as well as a pointer. It sets that pointer to point to the start of the pixel data. You can then access pixels directly via the pointer.
It'll end up looking something like this (note this is all from memory, double check all the parameters and stuff with CreateDIBSection documentation):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
BITMAPINFO info;
info.biheader.<stuff> = <stuff>; // set 'info' accordingly
unsigned char* pixels; // the pointer that will point to pixels
HBITMAP bmp = CreateDIBSection( ..., &info, ..., (void**)(&pixels), ... );
if(!bmp)
{
// failed to create bitmap
}
// here, 'pixels' points to the pixel data
// say you want to change the pixel at 100,50 to be full green:
int index = 50 * pitch; // 'pitch' is as explained above
index += 100 * bytes_per_pixel; // bytes per pixel is 4 for 32-bit bitmaps
pixels[index + 0] = 0; // red (or blue, I forget)
pixels[index + 1] = 255; // green
pixels[index + 2] = 0; // blue (or red, I forget)
|
That's it. Writing to 'pixels' directly changes the pixel on the bitmap.
From there, if you want to actually display it, you put that bitmap in a DC, and BitBlt it to the display (or to another DC).