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 ...
如何使用CImage从字节*数组加载图像?到目前为止,我的解决方案是创建一个临时文件,但是这个操作有时非常昂贵……可能有库,但我不想链接到其他库,我只需要获得图像大小并有效地显示到屏幕上,CImage是我所需要的…
p->pbData is a BYTE* array and p->dwDataLen is a DWORD that hold the array size
p->pbData是一个字节*数组,而p->dwDataLen是一个保存数组大小的DWORD
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();`
3 个解决方案
#1
3
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
2
You could do it with the CImage::Load(IStream*) function. You can get the IStream from CreateStreamOnHGlobal() or create your own.
您可以使用CImage::Load(IStream*)函数来实现。您可以从CreateStreamOnHGlobal()获得IStream,也可以创建自己的IStream。
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.
请注意,使用该文件实际上是更好的解决方案。CImage创建一个内存映射文件,以从图像中请求并加载像素。当从内存加载时,需要加倍内存,这会对分页文件造成压力。如果图像很大,你会遇到麻烦。
#3
0
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..;)
因此,上面的解决方案无法工作,除非堆缓冲区包含文件路径。
#1
3
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
2
You could do it with the CImage::Load(IStream*) function. You can get the IStream from CreateStreamOnHGlobal() or create your own.
您可以使用CImage::Load(IStream*)函数来实现。您可以从CreateStreamOnHGlobal()获得IStream,也可以创建自己的IStream。
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.
请注意,使用该文件实际上是更好的解决方案。CImage创建一个内存映射文件,以从图像中请求并加载像素。当从内存加载时,需要加倍内存,这会对分页文件造成压力。如果图像很大,你会遇到麻烦。
#3
0
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..;)
因此,上面的解决方案无法工作,除非堆缓冲区包含文件路径。