Load image from memory buffer using ATL CImage

孤街醉人 提交于 2019-12-05 18:27:05
bool Create(BYTE* heap, DWORD heap_len, CImage& img)
{
    bool ret = false;
    HGLOBAL hGlobal = ::GlobalAlloc(GHND, heap_len);
    LPBYTE  lpByte  = (LPBYTE)::GlobalLock(hGlobal);
    CopyMemory(lpByte, heap, heap_len);
    ::GlobalUnlock(hGlobal);
    IStream* pStream = NULL;
    if ( SUCCEEDED(::CreateStreamOnHGlobal(hGlobal, FALSE, &pStream)) )
    {
        img.Destroy();
        img.Load(pStream);
        pStream->Release();
        ret = true;
    }
    GlobalFree(hGlobal);
    return ret;
}

You could do it with the CImage::Load(IStream*) function. You can get the IStream from CreateStreamOnHGlobal() or create your own.

Beware that using the file is actually the superior solution. CImage creates a memory-mapped file to demand-load the pixels from the image. When you load from memory you need double the memory and you'll put pressure on the paging file. And you'll run into trouble if the image is large.

Rolf
HRESULT Load(
   IStream* pStream
) throw();

pStream A pointer to a stream containing the name of the image file to load.

So the solutions above can't work, unless the the heap buffer consists of the File Path..;)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!