Loading an image from a memory buffer using ATL CImage

How to load an image from a BYTE * array using CImage? My workaround so far is simply to create a temporary file, but this operation is very expensive sometimes ... There are probably libraries for this, but I do not want to reference other libraries, all I need is to get the image size and display it efficiently it on the screen, and CImage is all I need for this ...

p-> pbData is an BYTE * array, and p-> dwDataLen is a DWORD that contains the size of the array

My code is:

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();` 
+3
source share
3 answers
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;
}
+3
source

CImage:: Load (IStream *). IStream CreateStreamOnHGlobal() .

, . CImage , . , , . , .

+2
HRESULT Load(
   IStream* pStream
) throw();

pStream , .

, , .;)

0

Source: https://habr.com/ru/post/1760348/


All Articles