Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CImage read and write to memory files

368 views
Skip to first unread message

phil oakleaf

unread,
Aug 18, 2009, 5:35:33 AM8/18/09
to
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

Goran

unread,
Aug 18, 2009, 6:27:55 AM8/18/09
to
On Aug 18, 11:35 am, phil oakleaf <n...@oakleafsoftware.co.uk> wrote:
> 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.

That's what

HRESULT Save(IStream* pStream, REFGUID guidFileType);

is for.

You have to create an object implementing implement IStream interface,
but that would be the easy part.

HTH,
Goran.

Pavel Pokutnev

unread,
Aug 18, 2009, 7:01:21 AM8/18/09
to

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;
}

0 new messages