Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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

[code]
#define WIDTH 5
#define HEIGHT 5

void
SetupImageTransfer(void) {

BITMAPINFOHEADER bmpih;
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);
if(!hbmp)
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
[/code]

Comments

  • Malcolm_McLeanMalcolm_McLean Posts: 241Member
    : 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.

  • yakmanyakman Posts: 4Member
    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.