Available in the LEADTOOLS Imaging toolkit. |
LoadMemory example for Visual J++
This example uses WIN16 C DLL calls to load a file into memory, then uses the LoadMemory method to load the memory-resident file into a bitmap.
// Declare WIN32 C DLL constants and functions in the private section of your main form.
private final int OF_READ = 0x0;
private final int OF_CREATE = 0x1000;
private final int SEEK_SET = 0;
private final int SEEK_END = 2;
private final int GMEM_MOVEABLE = 0x2;
/** @dll.struct() */
class OFSTRUCT
{
byte cBytes;
byte fFixedDisk;
short nErrCode;
short Reserved1;
short Reserved2;
/** @dll.structmap([type=TCHAR[128]]) */
String szPathName;
}
static /** @dll.import("KERNEL32") */
class KernelAPIs
{
public static native int OpenFile( String lpFileName, OFSTRUCT lpReOpenBuff, int wStyle );
public static native int _hread( int hFile, int lpBuffer, int wBytes );
public static native int _hwrite( int hFile, int lpBuffer, int lBytes );
public static native int _lclose( int hFile );
public static native int _llseek( int hFile, int lOffset, int iOrigin );
public static native int GlobalAlloc( int wFlags, int dwBytes );
public static native int GlobalFree( int hMem );
public static native int GlobalLock ( int hMem );
public static native int GlobalUnlock ( int hMem );
}
private void LoadMemory_click(Object source, Event e)
{
// Load the file into memory.
OFSTRUCT of = new OFSTRUCT();
int hf = KernelAPIs.OpenFile("c:\\lead\\images\\image1.cmp", of, OF_READ );
if( hf == -1 )
{
MessageBox.show( "Error opening file!" );
return;
}
int iSize = KernelAPIs._llseek( hf, 0, SEEK_END );
KernelAPIs._llseek( hf, 0, SEEK_SET );
int hMem = KernelAPIs.GlobalAlloc( GMEM_MOVEABLE, iSize );
if( hMem == -1 )
{
KernelAPIs._lclose( hf );
MessageBox.show( "Not enough memory to hold the file in memory!" );
return;
}
int lpMem = KernelAPIs.GlobalLock( hMem );
if( lpMem == 0 )
{
KernelAPIs._lclose( hf );
KernelAPIs.GlobalFree( hMem );
MessageBox.show( "Not enough memory to hold the file in memory!" );
return;
}
if( KernelAPIs._hread( hf, lpMem, iSize ) != iSize )
{
KernelAPIs._lclose( hf );
KernelAPIs.GlobalUnlock( hMem );
KernelAPIs.GlobalFree( hMem );
MessageBox.show( "Error reading file" );
return;
}
KernelAPIs._lclose( hf );
KernelAPIs.GlobalUnlock( hMem );
// Load the bitmap from the memory-resident file.
if( LEAD1.LoadMemory( hMem, (short) 0, (short) 0, -1, iSize ) != 0 )
MessageBox.show( "Error calling LoadMemory" );
else
MessageBox.show( "LoadMemory succeeded" );
KernelAPIs.GlobalFree( hMem );
}