如何使用CreateDIBSection在BITMAPINFO中写入颜色数据?

How to write color data in BITMAPINFO by use CreateDIBSection?

首先我在 UINT** framebuffer 中写了一些 UINT 作为颜色,然后通过 CreateDIBSection 创建了一个 BITMAPINFO,但是在 运行 程序之后 window 是黑色而不是我设置的一些颜色,怎么了?

 PAINTSTRUCT ps;
        HDC hdc;

    static int s_widthClient, s_heightClient;   
    static BITMAPINFO s_bitmapInfo;
    static HDC s_hdcBackbuffer;     
    static HBITMAP s_hBitmap;
    static HBITMAP s_hOldBitmap;
    static void* s_pData;           

    switch (message)
    {
    case WM_CREATE:
    {

        RECT rc;
        GetClientRect(hWnd, &rc);
        s_widthClient = rc.right - rc.left;
        s_heightClient = rc.bottom - rc.top;

        Tiny3DDevice pDevice(s_widthClient, s_heightClient, s_pData);
        pDevice.Test();

        BITMAPINFOHEADER bmphdr = { 0 };
        bmphdr.biSize = sizeof(BITMAPINFOHEADER);
        bmphdr.biWidth = s_widthClient;
        bmphdr.biHeight = -s_heightClient;
        bmphdr.biPlanes = 1;
        bmphdr.biBitCount = 32;
        bmphdr.biSizeImage = s_heightClient * s_widthClient * 4;

        s_hdcBackbuffer = CreateCompatibleDC(nullptr);

        HDC hdc = GetDC(hWnd);
        //s_hBitmap = CreateCompatibleBitmap(hdc, s_widthClient, s_heightClient);
        s_hBitmap = CreateDIBSection(nullptr, (PBITMAPINFO)&bmphdr, DIB_RGB_COLORS,
            reinterpret_cast<void**>(&pDevice.m_pFramebuffer), nullptr, 0);

        s_hOldBitmap = (HBITMAP)SelectObject(s_hdcBackbuffer, s_hBitmap);
        ReleaseDC(hWnd, hdc);
    }
    break;
    case WM_PAINT:
    {
        hdc = BeginPaint(hWnd, &ps);
        //BitBlt(s_hdcBackbuffer, 0, 0, s_widthClient, s_heightClient, nullptr, 0, 0, WHITENESS);
        ////draw text
        //SetTextColor(s_hdcBackbuffer, RGB(0, 0, 0));
        //SetBkMode(s_hdcBackbuffer, TRANSPARENT);
        //TextOut(s_hdcBackbuffer, 0, 5, text.c_str(), text.size());

        BitBlt(ps.hdc, 0, 0, s_widthClient, s_heightClient, s_hdcBackbuffer, 0, 0, SRCCOPY);
        EndPaint(hWnd, &ps);
    }
        break;

和 Tiny3DDevice:

    class Tiny3DDevice
    {
    public:
        Tiny3DDevice(int width, int height, void *fb);
        ~Tiny3DDevice();
    public:
        void Test();    
    public:
        int m_width;
        int m_height;
        UINT** m_pFramebuffer;
    };
Tiny3DDevice::Tiny3DDevice(int width, int height, void *fb)
{
    m_width = width;
    m_height = height;
    m_pFramebuffer = new UINT*[width];
    for (int i = 0; i < width; ++i)
    {
        m_pFramebuffer[i] = new UINT[height];
    }
}

void Tiny3DDevice::Test()
{
    ZCFLOAT3 color(0.5f, 0.5f, 0.5f);
    for (int i = 0; i < m_width; ++i)
        for (int j = 0; j < m_height; ++j)
        {
            //m_pFramebuffer[i][j] = MathUtil::ColorToUINT(color);
            m_pFramebuffer[i][j] = 0x3fbcefff;
        }
}

怎么了?我应该如何在 m_framebuffer? 中写入数据?知道吗?

你需要这样的代码

PVOID pv;
if (s_hBitmap = CreateDIBSection(nullptr, (PBITMAPINFO)&bmphdr, DIB_RGB_COLORS, &pv, 0, 0))
{
    RtlFillMemoryUlong((PULONG)pv, bmphdr.biSizeImage, 0x3fbcefff);
}

您不需要在您的代码中分配 pv(pDevice.m_pFramebuffer),因为它在 CreateDIBSection 中分配。你只需要填写它。您的 Tiny3DDevice 代码完全错误且毫无意义。 对 s_hdcBackbuffer 等使用静态变量 - 噩梦

BITMAPINFOHEADER bmphdr = { 0 };

没有人尝试使用这个 :) ?

BITMAPINFOHEADER bmphdr = {  };