Available in the LEADTOOLS Imaging toolkit. |
LoadMemory example for C++ 4.0 and later
This example uses WIN32 API calls to load a file into memory, then uses the LoadMemory method to load the memory-resident file into a bitmap.
void CTutorDlg::OnLoadMemory()
{
HFILE hf;
HGLOBAL hMem;
LPSTR lpMem;
long iSize;
OFSTRUCT of;
if( (hf = OpenFile( "c:\\lead\\images\\image1.cmp", &of, OF_READ )) == HFILE_ERROR )
{
MessageBox( "Error opening file!" );
return;
}
iSize = _llseek( hf, 0, SEEK_END );
_llseek( hf, 0, SEEK_SET );
if( (hMem = GlobalAlloc( GMEM_MOVEABLE, iSize)) == NULL )
{
_lclose( hf );
MessageBox( "Not enough memory to hold the file in memory!" );
return;
}
if( (lpMem = (LPSTR)GlobalLock(hMem)) == NULL )
{
_lclose( hf );
GlobalFree( hMem );
MessageBox( "Not enough memory to hold the file in memory!" );
return;
}
if( _hread( hf, lpMem, iSize) != iSize )
{
_lclose( hf );
GlobalUnlock( hMem );
GlobalFree( hMem );
MessageBox( "Error reading file" );
return;
}
_lclose( hf );
GlobalUnlock( hMem );
if( m_Lead.LoadMemory( (OLE_HANDLE)hMem, 0, 0, -1, iSize ) )
MessageBox( "Error calling LoadMemory" );
else
MessageBox( "LoadMemory succeeded" );
GlobalFree( hMem );
}