Available in the LEADTOOLS Imaging toolkit. |
SaveMemory example for C++ 4.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 C DLL calls to save the file to disk and free the memory.
void CTutorDlg::OnSaveMemory()
{
HGLOBAL hMem;
long iSize;
LPSTR lpMem;
HFILE hf;
OFSTRUCT of;
char szText[256];
if( m_Lead.SaveMemory( (long FAR *)&hMem, FILE_CMP, 24, -1,
&iSize ) ) {
MessageBox( "Error calling SaveMemory" );
return;
}
if( m_Lead.GetMemoryInfo((OLE_HANDLE)hMem, 0, iSize, 0) )
{
GlobalFree (hMem);
MessageBox( "Error calling GetMemoryInfo" );
return;
}
/* Ask for a confirmation to save this file */
wsprintf( szText, "File info: \r %u x %u x %u BPP \rSize in memory: %lu
\rSize on disk %lu",
(unsigned int)m_Lead.GetInfoWidth(), (unsigned
int)m_Lead.GetInfoHeight(), (unsigned int)m_Lead.GetInfoBits(),
(unsigned long)m_Lead.GetInfoSizeMem(), (unsigned
long)m_Lead.GetInfoSizeDisk()
);
if( MessageBox(szText, "Write this file on disk ?", MB_YESNO) != IDYES )
{
GlobalFree (hMem);
return;
}
if( (lpMem = (LPSTR)GlobalLock(hMem)) == NULL )
{
GlobalFree( hMem );
MessageBox( "Error calling GlobalLock" );
return;
}
if( (hf = OpenFile( "c:\\lead\\images\\savemem.cmp", &of, OF_CREATE)) ==
HFILE_ERROR ) {
GlobalUnlock( hMem );
GlobalFree( hMem );
MessageBox( "Error calling GlobalLock" );
return;
}
if( _hwrite( hf, lpMem, iSize ) != iSize )
MessageBox( "Error calling GlobalLock" );
else
MessageBox( "c:\\lead\\images\\savemem.cmp was saved successfully on disk." );
_lclose( hf );
GlobalUnlock( hMem );
GlobalFree( hMem );
}