从像素缓冲区创建HBITMAP然后渲染

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从像素缓冲区创建HBITMAP然后渲染相关的知识,希望对你有一定的参考价值。

好吧,所以我真的开始使用WinAPI绘制位图图像。所以,如果我的代码是绝对垃圾,我道歉。无论如何,我该怎么做呢?基本上,我想使用像这样的HBITMAP从一个简单的unsigned char像素缓冲区创建一个CreateBitmap()对象...

HBITMAP hbm = CreateBitmap(width, height, 1, 24, buffer);

(显然这是每像素3个字节)

这似乎工作正常,直到我尝试渲染它。现在,我只是想保持简单,并在屏幕上绘制一个100×100的黑色像素正方形,但没有任何显示。如果有人能指出我残暴的代码中的错误,我将不胜感激。

这是我的设置(我知道很多事情都可以改进我现在想尽可能简约,实际上有些东西出现在屏幕上):

HBITMAP hbm = NULL;

void window_init(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {

    static unsigned char buffer[30000] = {0};
    hbm = CreateBitmap(100, 100, 1, 24, buffer);

}
void window_draw(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {

    HDC hdc = BeginPaint(hwnd, &ps);
    HDC hdcMem = CreateCompatibleDC(hdc);

    HBITMAP hbmOld = SelectObject(hdcMem, hbm);
    BitBlt(hdc, 100, 100, 100, 100, hdcMem, 0, 0, SRCCOPY);

    SelectObject(hdcMem, hbmOld);
    DeleteDC(hdcMem);

    EndPaint(hwnd, &ps);

}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch (msg) {

        case WM_CREATE: window_init(hwnd, msg, wParam, lParam); break;
        case WM_PAINT: window_draw(hwnd, msg, wParam, lParam); break;
        case WM_CLOSE: DestroyWindow(hwnd); break;
        case WM_DESTROY: PostQuitMessage(0); break;
        default: return DefWindowProc(hwnd, msg, wParam, lParam);

    }

    return 0;

}

这是我的整个代码,以防你要复制它...

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>

typedef struct {

    const char *wc_name;
    const char *title;

    unsigned int width;
    unsigned int height;

} Window;

static HBITMAP hbmp = NULL;
static PAINTSTRUCT ps;

void window_create(Window *window, const char *wc_name, const char *title, unsigned int width, unsigned int height) {

    window->wc_name = wc_name;
    window->title = title;

    window->width = width;
    window->height = height;

}
void window_init(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {

    static unsigned char buffer[30000] = {0};
    hbmp = CreateBitmap(100, 100, 1, 24, buffer);

}
void window_draw(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {

    HDC hdc = BeginPaint(hwnd, &ps);
    HDC hdcMem = CreateCompatibleDC(hdc);

    HBITMAP hbmOld = SelectObject(hdcMem, hbmp);
    BitBlt(hdc, 100, 100, 100, 100, hdcMem, 0, 0, SRCCOPY);

    SelectObject(hdcMem, hbmOld);
    DeleteDC(hdcMem);;

    EndPaint(hwnd, &ps);

}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch (msg) {

        case WM_CREATE: window_init(hwnd, msg, wParam, lParam); break;
        case WM_PAINT: window_draw(hwnd, msg, wParam, lParam); break;
        case WM_CLOSE: DestroyWindow(hwnd); break;
        case WM_DESTROY: PostQuitMessage(0); break;
        default: return DefWindowProc(hwnd, msg, wParam, lParam);

    }

    return 0;

}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {

    Window window;
    window_create(&window, "Parent Window", "My Window", 1000, 1000 / 16 * 9);

    WNDCLASSEX wc = {0};
    MSG msg = {0};
    HWND hwnd = NULL;

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WndProc;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
    wc.lpszClassName = window.wc_name;
    wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

    if (!RegisterClassEx(&wc)) {

        MessageBox(NULL, "Failed to register application window!", "WC Registration Error!", MB_ICONEXCLAMATION | MB_OK);
        return -1;

    }

    hwnd = CreateWindowEx(

        0, window.wc_name, window.title,
        WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
        window.width, window.height, NULL, NULL, hInstance, NULL

    );

    if (!hwnd) {

        MessageBox(NULL, "Failed to launch application window!", "HWND Creation Error!", MB_ICONEXCLAMATION | MB_OK);
        return -1;

    }

    ShowWindow(hwnd, SW_MAXIMIZE);
    UpdateWindow(hwnd);

    while (GetMessage(&msg, NULL, 0, 0) > 0) {

        TranslateMessage(&msg);
        DispatchMessage(&msg);

    }

    return msg.wParam;

}
答案

CreateBitmap不同,CreateDIBSection生成设备相关位图的句柄,如果成功,保证与设备上下文一起创建与设备无关的位图,并允许您访问位图中的实际位,这与CreateCompatibleBitmap不同。

如何使用CreateDIBSection

HDC hdc = GetDC(hwnd);
BITMAPINFO bi = { 0 };
static BYTE *bits = NULL;
static unsigned char buffer[30000] = { 0 };

bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bi.bmiHeader.biWidth = 100;
bi.bmiHeader.biHeight = 100;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 24;
bi.bmiHeader.biCompression = BI_RGB;

hbmp = CreateDIBSection(hdc, &bi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
CopyMemory(bits, buffer, 30000); // Can be replaced with arbitrary RGB value population
ReleaseDC(hwnd, hdc);

以上是关于从像素缓冲区创建HBITMAP然后渲染的主要内容,如果未能解决你的问题,请参考以下文章

WebGL - 从渲染缓冲区读取像素数据

对 BITMAPINFO (BITMAP) 中的像素数据的原始/直接访问

从片段着色器中的地形高程数据计算法线

渲染管道光栅阶段一“总览”

渲染管道光栅阶段一“总览”

在着色器中使用 glTexture