Load image from memory buffer using ATL CImage

久未见 提交于 2019-12-07 13:40:24

问题


How can I load an image from BYTE* array using CImage ? My workaround until now is to simply create a temporary file, but this operation is very expensive sometimes ... There are probably libraries for that, but I do not want to link to other libraries, all I need is to get image size and effectively display to screen, and CImage is all I need for that ...

p->pbData is a BYTE* array and p->dwDataLen is a DWORD that hold the array size

My code :

ATL::CAtlTemporaryFile TempFile;  
TempFile.Create(NULL, GENERIC_WRITE | GENERIC_READ);  
TempFile.Write(p->pbData, p->dwDataLen);  
TempFile.HandsOff();  
ATL::CImage m_image;  
m_image.Load(TempFile.TempFileName());  
    TempFile.Close();
int h = m_image.GetHeight();  
int w = m_image.GetWidth();  
// rest of code here

    m_image.Destroy();  
m_image.ReleaseGDIPlus();` 

回答1:


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;
}



回答2:


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.




回答3:


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..;)



来源:https://stackoverflow.com/questions/3514275/load-image-from-memory-buffer-using-atl-cimage

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