Available in the LEADTOOLS Imaging toolkit. |
LoadMemory example for Access 95 and 97
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.
' Declare WIN32 API constants and functions in the Declarations module.
Private Const OF_READ = &H0
Private Const OF_CREATE = &H1000
Private Const SEEK_SET = 0 ' seek to an absolute position
Private Const SEEK_END = 2 ' seek relative to end of file
Private Const GMEM_MOVEABLE = &H2
Private Const OFS_MAXPATHNAME = 128
Private Type OFSTRUCT
cBytes As Byte
fFixedDisk As Byte
nErrCode As Integer
Reserved1 As Integer
Reserved2 As Integer
szPathName(OFS_MAXPATHNAME) As Byte
End Type
Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
Private Declare Function hread Lib "kernel32" Alias "_hread" (ByVal hFile As Long, ByVal lpBuffer As Long, ByVal lBytes As Long) As Long
Private Declare Function hwrite Lib "kernel32" Alias "_hwrite" (ByVal hFile As Long, ByVal lpBuffer As Long, ByVal lBytes As Long) As Long
Private Declare Function lclose Lib "kernel32" Alias "_lclose" (ByVal hFile As Long) As Long
Private Declare Function llseek Lib "kernel32" Alias "_llseek" (ByVal hFile As Long, ByVal lOffset As Long, ByVal iOrigin As Long) As Long
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
' ---------------------- Code for LoadMemory (32 bit) -------------
Private Sub Form_Activate()
DoCmd.Maximize
Me.ScrollBars = False
Me.Painting = True
Dim hf As Long
Dim hMem As Long
Dim lpMem As Long
Dim iSize As Long
Dim of As OFSTRUCT
Dim nRet As Long
' Load the file into memory.
hf = OpenFile("c:\lead\images\image1.cmp", of, OF_READ)
If hf = -1 Then
MsgBox ("Error opening file!")
GoTo quit_loadmemory
End If
iSize = llseek(hf, 0, SEEK_END)
nRet = llseek(hf, 0, SEEK_SET)
hMem = GlobalAlloc(GMEM_MOVEABLE, iSize)
If hMem = -1 Then
lclose (hf)
MsgBox ("Not enough memory to hold the file in memory!")
GoTo quit_loadmemory
End If
lpMem = GlobalLock(hMem)
If lpMem = 0 Then
lclose (hf)
GlobalFree (hMem)
MsgBox ("Not enough memory to hold the file in memory!")
GoTo quit_loadmemory
End If
If hread(hf, lpMem, iSize) < iSize Then
lclose (hf)
GlobalUnlock (hMem)
GlobalFree (hMem)
MsgBox ("Error reading file")
GoTo quit_loadmemory
End If
lclose (hf)
GlobalUnlock (hMem)
'Load the bitmap from the memory-resident file.
If Lead1.LoadMemory(hMem, 0, 0, -1, iSize) <> 0 Then
MsgBox ("Error calling LoadMemory")
Else
MsgBox ("LoadMemory succeeded")
End If
GlobalFree (hMem)
quit_loadmemory:
End Sub