Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render Buffer on Screen in Windows

I'm searching for a way rendering a char buffer onto the content-area of a window. This is just pseudo but intended to demonstrate what I actually 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 similar?

like image 963
Niklas R Avatar asked Dec 13 '22 03:12

Niklas R


1 Answers

I think you need to create a device independent bitmap (DIB). If you already have an array of pixels that is ready to be put on an application window, you may need to copy the whole array to the buffer allocated by the CreateDIBSection API and call BitBlt to transfer the DIB to the window. This is the only way I know to show a mere array of pixels as a visible picture on the computer screen on Win32 platform and it is highly complicated and difficult to understand.

Here are the steps I used to take to test things similar to what you want to do:

Creating DIB:

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);

Implementing WM_PAINT event handler (the hWnd variable holds 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 don't really expect the above code snippets would directly help you. If you are determined to use GDI functions like ones in the above snippets, I recommend you to read very carefully their API documents on MSDN. Because it is very tricky to properly release or delete DCs or GDI objects acquired during using the APIs.

like image 53
M. Shiina Avatar answered Dec 15 '22 00:12

M. Shiina