创建从内存缓冲区HBITMAP [英] Creating HBITMAP from memory buffer

查看:2569
本文介绍了创建从内存缓冲区HBITMAP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些加载BLOB数据出来,可以重新present PNG格式或原始的二进制数据对各种位图和图标数据库的应用程序。这是被存储在一个的std ::矢量< unsigned char型>

I have an application which loads some blob data out of a database which can represent png formatted or raw binary data for various bitmaps and icons. This is being stored in a std::vector<unsigned char>

我用 CImageList 对象,以显示树视图,工具栏图像等各种图像,但问题是创造从内存中的数据位图出来的模糊如果它做的东西时,象下面这样丢失的像素:

I'm using CImageList objects to display various images in tree views, toolbar images, etc but the problem is creating bitmaps from the data in memory are coming out fuzzy as if it's missing pixels when doing something like below:

std::vector<unsigned char> bits;
HBITMAP hbitmap = CreateBitmap(16, 16, 1, 32, bits.data());

要解决此问题,现在我在矢量数据()只是写出到一个临时文件,然后使用的LoadImage读回并创造不同于HBITMAP。这工作完全是但无可否认是一个无耻的黑客,应该是完全没有必要我希望。

To work around this issue for now I am simply writing out the data() in the vector to a temporary file and then using LoadImage to read it back in and creating the HBITMAP from that. This works perfectly however that is admittedly a shameless hack and should be completely unnecessary I would hope.

我在网上看了看四周,但还没有找到如何正确,从内存中创建hbitmaps任何真正的好例子。我希望能够创建这些位图要添加到图像列表没有任何文件I / O和数量有限,如果可能的各地复制数据。

I've looked around online but haven't found any really good examples of how to "properly" create hbitmaps from memory. I would like to be able to create these bitmaps to be added to the image list without any file i/o and limited amounts of copying the data around if possible.

寻找最好的方式做到这一点,显然Windows特定code是罚款。

Looking for the best way to do this and obviously windows specific code is fine.

更新:

根据JDV的答案,我开始与CreateCompatibleBitmap,CreateDIBitmap,终于CreateDIBSection播放。所有这些结束了创建可爱的黑位图,而不是previous模糊的位图,所以我一定是错了再做东西,我的猜测是因为此位图创建是正在一个没有屏幕的DC或窗口的概念,一个对象来完成在使用的GetDC(NULL) CreateCompatibleDC(NULL)都没有好。示例code:

Based on jdv's answer I began playing with CreateCompatibleBitmap, CreateDIBitmap, and finally CreateDIBSection. All of these ended up creating lovely black bitmaps instead of the previous fuzzy bitmaps so I must be doing something wrong again and my guess is since this bitmap creation is being done in an object that has no concept of the screen dc or window that using GetDC(NULL) and CreateCompatibleDC(NULL) are no good. Sample code:

    BITMAPINFO bmi;
    ZeroMemory(&bmi, sizeof(BITMAPINFO));
    bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmi.bmiHeader.biBitCount = 32;
    bmi.bmiHeader.biHeight = 16;
    bmi.bmiHeader.biWidth = 16;
    bmi.bmiHeader.biPlanes = 1;

    HDC dc = CreateCompatibleDC(NULL);
    HBITMAP hbitmap = CreateDIBSection(dc, &bmi, DIB_RGB_COLORS, (void**)blobData.GetMember<FILEDATAFIELD_DATA>().data(), NULL, 0);

我现在的思然也一定是一个简单的去这也许是避免HBITMAP干脆直接与的CBitmap 类工作方式?当它归结为图像添加到 CImageList 我用的CBitmap :: FromHandle(HBITMAP HBITMAP,COLORREF面罩)反正。有谁知道一个简单的方法来初始化从的std ::矢量&lt一个的CBitmap 对象; unsigned char型&GT;

I am now of course thinking there has got to be a simpler way to go about this perhaps by avoiding the HBITMAP altogether and working directly with CBitmap class? When it comes down to adding the image to the CImageList I'm using CBitmap::FromHandle(HBITMAP hbitmap, COLORREF mask) anyway. Does anyone know a simple way to initialize a CBitmap object from a std::vector<unsigned char>?

推荐答案

使用GdiPlus我有一些作品pretty好,不涉及任何拔牙齿!

Using GdiPlus I got something that works pretty well and doesn't involve pulling any teeth!

Gdiplus::Bitmap* pBitmap = NULL;
IStream* pStream = NULL;

HRESULT hResult = ::CreateStreamOnHGlobal( NULL, TRUE, &pStream );
if(hResult == S_OK && pStream)
{
    hResult = pStream->Write(&bits[0], ULONG(bits.size()), NULL);
    if(hResult == S_OK)
        pBitmap = Gdiplus::Bitmap::FromStream(pStream);
    pStream->Release();
}

编辑:每Jegatheesh更改

Changed per Jegatheesh

这篇关于创建从内存缓冲区HBITMAP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆