Render Buffer on screen on Windows

I'm looking for a way to create a char buffer in the content area of ​​a window. This is just pseudo, but intended to demonstrate what I really want to do:

char buffer[300][200][3];    // 300px x 200px x RGB bytes
// ... render stuff into buffer
FancyWindowsFunctionToRenderBufferOnWindow(my_hwnd, buffer, 300, 200, offset_x, offset_y);

      

Is there a way to do something like this?

+3


source to share


2 answers


I think you need to create a device independent bitmap (DIB). If you already have a pixel array ready to be placed in the application window, you may need to copy the entire array to the buffer allocated by the CreateDIBSection API and call BitBlt to wrap the DIB into the window. This is the only way I know to show a simple array of pixels as a visible image on a Win32 computer screen, and it is very difficult and difficult to understand.

Here are the steps I used to check what you want to do:

DIB creation:

BITMAPINFO bmi;
memset(&bmi, 0, sizeof(bmi));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = /* Width of your image buffer */
bmi.bmiHeader.biHeight = - /* Height of your image buffer */
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;

HDC hDesktopDC = GetDC(GetDesktopWindow());
HBITMAP hDib = CreateDIBSection(hDesktopDC, &bmi, DIB_RGB_COLORS, (void **)&buffer, 0, 0);
if (buffer == NULL) { /* ERROR */ }
HDC hDibDC = CreateCompatibleDC(hDesktopDC);
HGDIOBJ hOldObj = SelectObject(hDibDC, hDib);

/* Copy your array of pixels to buffer allocated above. */

ReleaseDC(GetDesktopWindow(), hDesktopDC);

      



Implementation of the WM_PAINT event handler (the hWnd variable contains the window handle below):

case WM_PAINT:
    PAINTSTRUCT paint;
    HDC hWndDc = BeginPaint(hWnd, &paint);
    BitBlt(hWndDC, 0, 0, /* Width of DIB */, /* Height of DIB */,
           /* HDC of DIB (hDibDC in the above) */, 0, 0, SRCCOPY);
    EndPaint(hWnd, &paint);
    break;

      

I really don't expect the above code snippets to help you. If you choose to use GDI features like the ones in the snippets above, I recommend that you read their API docs on MSDN carefully. Because it is very difficult to properly release or remove DC files or GDI objects obtained using APIs.

+6


source


It looks like you have an image (raster) that is stored as a character array (which is an odd choice since you usually want an unsigned array for raw bitmaps).

If you meet certain alignment constraints, you can directly display the bitmap using SetDIBits . You fill in a BITMAPINFO structure that describes the pixel format and dimensions of the image, and then you pass that along with your data to SetDIBits. He will paint them in DC. It can be a little tricky to get all the parameters correct.



The alignment requirement is that each scan line must start at a 4-byte boundary. If you don't meet this requirement, you will end up with garbage that looks like the wrong step. You can make a correctly aligned copy of the data if needed.

+5


source







All Articles