creating + modifying bitmap help

i need to be able to create a bitmap, and change its pixels

i know all about SetPixel(), but in my specific situation, i can only change them by changing a buffer in memory.

im writing a plugin for an application, and it needs the DC of the bitmap, but thats not that hard to get anyway.

also, is it possible to allocate the pixel memory myself, and not have the system allocate it.

this is my attempt so far, when i run it, the bitmap is black

#define WIDTH 5
#define HEIGHT 5

SetupImageTransfer(void) {

memset(&bmpih, 0, sizeof(bmpih));
bmpih.biSize = sizeof(BITMAPINFOHEADER);
bmpih.biWidth = WIDTH;
bmpih.biHeight = HEIGHT;
bmpih.biPlanes = 1;
bmpih.biBitCount = 24;
bmpih.biCompression = BI_RGB;
bmpih.biSizeImage = ((((WIDTH * 24) + 31) & ~31) >> 3) * HEIGHT;

HDC dc = CreateCompatibleDC(NULL);

char* bmp_data;
HBITMAP hbmp = CreateDIBSection(dc, (BITMAPINFO*)&bmpih, DIB_RGB_COLORS, (void**)&bmp_data, NULL, 0);
printf("cdibs error <%d>
", GetLastError());

char* data = malloc(bmpih.biSizeImage);

//how many bytes of padding we need at the end of each line
int pad_count = ((((WIDTH * 24) + 31) & ~31) >> 3) - (WIDTH * 3);

//fills everything with blue, but this is only a proof of concept
//so that later any pixel can be filled with any color
int f = 0;
for(int h = 0; h < HEIGHT; h++) {
for(int w = 0; w < WIDTH; w++) {
data[f++] = 'xff'; //blue
data[f++] = 0; //green
data[f++] = 0; //red
memset(data + f, 0, pad_count);
f += pad_count;

SetDIBits(dc, hbmp, 0, HEIGHT, data, (BITMAPINFO*)&bmpih, DIB_RGB_COLORS);
SelectObject(dc, hbmp);
//now pass the DC to the application


  • : i need to be able to create a bitmap, and change its pixels
    : this is my attempt so far, when i run it, the bitmap is black
    I can't see anything wrong, but I wouldn't create a 5x5 bitmap at first. Create a 32x32 to get round the problem of padding, and see if you can get a blue image on screen. Then check whether you padding calculations are correct.

  • yeah fixed it, it was some issue with compiling, the problem wasnt in the code i posted. But i never would have realised if you didnt say the code seemed alright.

    im quite sure about the padding calculations, i got that formula from msdn, just edited it a tiny bit.

    Although, when i try to update the bitmap pixels, i need to call SetDIBits() for the changes to take effect.
    Is there a way to edit the bitmap without calling anything after?
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!