如何在c++中使用GDI从内存中绘制RGB像素数据

How to draw RGB pixel data from memory with GDI in C++

本文关键字:RGB 内存 绘制 像素 数据 像素数 GDI c++      更新时间:2023-10-16

我有一个指向RGB数据(640x480x3字节)的指针,我想使用BitBlt或其他同样快的东西绘制到窗口中。我如何将RGB数据转换成可用的BitBlt(例如)。

这是我到目前为止尝试过的(没有成功)

    unsigned char *buf = theVI->getPixels(0);
    int size = theVI->getSize(0);
    int h = theVI->getHeight(0);
    int w = theVI->getWidth(0);
    HDC dc = GetDC(hwnd);
    HDC dcMem = CreateCompatibleDC(dc);
    HBITMAP bmp = CreateBitmap(w, h, 1, 24, buf);
    SelectObject(dcMem, bmp);
    BitBlt(dc, 0, 0, w, h, dcMem, 0, 0, SRCCOPY);

感谢

更新:这是工作代码…

    HDC dc = GetDC(hwnd);
    BITMAPINFO info;
    ZeroMemory(&info, sizeof(BITMAPINFO));
    info.bmiHeader.biBitCount = 24;
    info.bmiHeader.biWidth = w;
    info.bmiHeader.biHeight = h;
    info.bmiHeader.biPlanes = 1;
    info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    info.bmiHeader.biSizeImage = size;
    info.bmiHeader.biCompression = BI_RGB;
    StretchDIBits(dc, 0, 0, w, h, 0, 0, w, h, buf, &info, DIB_RGB_COLORS, SRCCOPY);
    ReleaseDC(hwnd, dc);

您可以使用StretchDIBits API函数将您的字节(即所谓的DIB设备独立位图)渲染到HDC。

在MSDN中检查DIB文章