SaveMemory example for C++ 5.0 and later
This example uses SaveMemory to save a bitmap to a memory-resident file, uses GetMemoryInfo to examine the file size; then uses WIN32 API calls to save the file to disk and free the memory.
void CTutorDlg::OnSaveMemory()
{
long hMem;
long iSize;
LPSTR lpMem;
HFILE hf;
TCHAR szText[256];
ILEADRasterIO *pRasterIO=NULL;
CoCreateInstance(CLSID_LEADRasterIO, NULL, CLSCTX_ALL,
IID_ILEADRasterIO, (void**)&pRasterIO);
if(pRasterIO->SaveMemory(m_LEADRasterView1.GetRaster(),
&hMem, FILE_CMP, 24,
(QFactorConstants)-1,
SAVE_OVERWRITE, &iSize ))
{
MessageBox(TEXT("Error calling SaveMemory"));
return;
}
if(pRasterIO->GetMemoryInfo(m_LEADRasterView1.GetRaster(),
hMem, 0, iSize))
{
GlobalFree((HGLOBAL)hMem);
MessageBox(TEXT("Error calling GetMemoryInfo"));
return;
}
/* Ask for a confirmation to save this file */
wsprintf(szText,
TEXT("File info: \r %u x %u x %u BPP \rSize in memory: %lu \rSize on disk %lu"),
(unsigned int)pRasterIO->GetInfoWidth(),
(unsigned int)pRasterIO->GetInfoHeight(),
(unsigned int)pRasterIO->GetInfoBits(),
(unsigned long)pRasterIO->GetInfoSizeMem(),
(unsigned long)pRasterIO->GetInfoSizeDisk());
if( MessageBox(szText, TEXT("Write this file on disk ?"), MB_YESNO) != IDYES )
{
GlobalFree((HGLOBAL)hMem);
return;
}
if( (lpMem = (LPTSTR)GlobalLock((HGLOBAL)hMem)) == NULL )
{
GlobalFree((HGLOBAL)hMem);
MessageBox(TEXT("Error calling GlobalLock"));
return;
}
#ifdef UNICODE
hf = _wopen (TEXT("d:\\temp\\savemem.cmp"), OF_CREATE);
#else
hf = _lopen ("d:\\temp\\savemem.cmp", OF_CREATE);
#endif
if( hf == 0 )
{
GlobalUnlock((HGLOBAL)hMem);
GlobalFree((HGLOBAL)hMem);
MessageBox(TEXT("Error calling GlobalLock"));
return;
}
if( _lwrite( hf, lpMem, iSize ) != (UINT)iSize )
MessageBox(TEXT("Error calling GlobalLock"));
else
MessageBox(TEXT("savemem.cmp was saved successfully on disk."));
_lclose( hf );
GlobalUnlock((HGLOBAL)hMem);
GlobalFree((HGLOBAL)hMem);
pRasterIO->Release();
}