0

In GDI+ we can create a direct bitmap like so: https://stackoverflow.com/a/34801225/16141581

We allocate an array of pixels, create a new bitmap and pass during the instanciation of the bitmap a pointer to the adress of the first element in the array.

We then can proceed to change the elements of the array, such that the changes are applied to the bitmap automatically.

How exactly can I reproduce this with HBITMAP in win32?

I am using the CreateBitmap function:

 HBITMAP hBitmap;
    uint32_t arr[240 * 240];
    //WndProc function
    LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
        switch (msg) {
    
        case WM_CREATE:
        {
// source: https://stackoverflow.com/a/56229028/16141581
            std::fill(arr, arr + sizeof(arr) / sizeof(*arr), RGB(255, 0, 0));
            hBitmap = CreateBitmap(240, 240, 1, 32, (void*)arr);
    
            return 0;
        }
    }

But changing elements of that particular array doesnt change the bitmap. Why not?

  • 3
    Have a look at [`CreateDIBSection()`](https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-createdibsection). It works the other way around. You don't create a bitmap from pixels, but the function gives you a pointer to the pixels, which you can use to directly manipulate the bitmap. Make sure to call `GdiFlush()` before manipulating the bitmap through the pointer, when you have drawn into the bitmap using any of the GDI APIs. – zett42 Jul 29 '22 at 22:40
  • 1
    See https://stackoverflow.com/a/54704832/7571258 – zett42 Jul 29 '22 at 22:44

0 Answers0