How can I load an image from BYTE* array using CIm开发者_Go百科age ? 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();`
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.
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..;)
精彩评论