如何在c++中使用GDI从内存中绘制RGB像素数据
How to draw RGB pixel data from memory with GDI in C++
我有一个指向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文章
相关文章:
- 将字符串存储在c++中的稳定内存中
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- Win32编译器选项和内存分配
- 当vector是tje全局变量时,c++中vector的内存管理
- 带内存和隔离功能的SQLite
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 迭代时从向量和内存中删除对象
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 为什么示例代码访问IUnknown中已删除的内存
- 如何在C++类内存结构中创建"spacer"?
- 从构造函数抛出异常时如何克服内存泄漏
- malloc() 可能出现内存泄漏
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- 在调用FreeLibrary后,释放动态链接到具有相同版本的CRT堆的DLL的内存
- 如何针对特定情况调试和修复此双自由内存损坏问题
- 类型总是使用其大小存储在内存中吗
- 要转换为 RGB 的 YUV 缓冲区的内存分配
- 如何在c++中使用GDI从内存中绘制RGB像素数据