array[byte] to HBITMAP or CBitmap(array[byte] 到 HBITMAP 或 CBitmap)
问题描述
我有一个字节数组(我直接从 .bmp 读取流,然后将其作为 BLOB 存储在数据库中),我想在 CImageList 中显示为图标.因此,我想以某种方式将我的数据加载到 HBITMAP 或 CBitmap 中.到目前为止,我已经这样做了,从文件中读取:
I have an array of bytes (which I read through a stream directly from a .bmp and then store as a BLOB in a database) which I want to display as icons in a CImageList. Therefore I want to somehow load my data into an HBITMAP or CBitmap. I have done it like this up to now, reading from a file:
hPic = (HBITMAP)LoadImage(NULL, strPath, IMAGE_BITMAP, dwWidth, dwHeight, LR_LOADFROMFILE | LR_VGACOLOR);
...
CBitmap bitmap;
bitmap.Attach(hPicRet);
但显然,这只适用于文件,但不适用于字节数组.如何获得相同的结果,但从字节数组中读取?
But obviously, that only works for files, but not for byte-arrays. How can I get the same result, but reading from an array of byte?
请注意,我的数组不仅包含颜色信息,还包含写入磁盘上的完整文件,包括所有标题和元数据.在我看来,丢弃所有这些信息是个坏主意.
Note that my array does not contain just the colour information, but rather the complete file as it is written on disk, including all headers and meta-data. It seems to me that discarding all that information is a bad idea.
推荐答案
假设您已将信息加载到名为 bytes....的 BYTE 数组中.
Assuming you have the information loaded into a BYTE array named bytes....
BITMAPFILEHEADER* bmfh;
bmfh = (BITMAPFILEHEADER*)bytes;
BITMAPINFOHEADER* bmih;
bmih = (BITMAPINFOHEADER*)(bytes + sizeof(BITMAPFILEHEADER));
BITMAPINFO* bmi;
bmi = (BITMAPINFO*)bmih;
void* bits;
bits = (void*)(bytes + bmfh->bfOffBits);
HDC hdc = ::GetDC(NULL);
HBITMAP hbmp = CreateDIBitmap(hdc, bmih, CBM_INIT, bits, bmi, DIB_RGB_COLORS) ;
::ReleaseDC(NULL, hdc);
这有点乱,可以使用大量的错误检查,但基本的想法是合理的.
It's a little messy and could use a hefty dose of error checking, but the basic idea is sound.
这篇关于array[byte] 到 HBITMAP 或 CBitmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:array[byte] 到 HBITMAP 或 CBitmap
基础教程推荐
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 初始化列表*参数*评估顺序 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- CString 到 char* 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
