tags:

views:

42

answers:

1

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  A: 

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.

Hans Passant