Available in the LEADTOOLS Imaging toolkit. |
SaveBuffer example for Visual Basic
'This sample loads the file 'szFileName', and saves it to memory buffer allocated by the user
'The original memory buffer is 1 bytes, which is too small to accommodate the image
'The SaveBuffer callback gets called as necessary, allowing the user to reallocate the memory buffer as needed
'When the image is completely saved in memory, it is loaded into a second LEAD control
'
'The following declarations are for calling Windows API's to allocate memory
'Private Declare Function HeapAlloc Lib "kernel32" (ByVal hHeap As Long, ByVal dwFlags As Long, ByVal dwBytes As Long) As Long
'Private Declare Function HeapReAlloc Lib "kernel32" (ByVal hHeap As Long, ByVal dwFlags As Long, ByVal lpMem As Long, ByVal dwBytes As Long) As Long
'Private Declare Function HeapFree Lib "kernel32" (ByVal hHeap As Long, ByVal dwFlags As Long, ByVal lpMem As Long) As Long
'Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
'Private Declare Function HeapCreate Lib "kernel32" (ByVal flOptions As Long, ByVal dwInitialSize As Long, ByVal dwMaximumSize As Long) As Long
'Private Declare Function HeapDestroy Lib "kernel32" (ByVal hHeap As Long) As Long
'
'The following variables are declared globally
'Dim lHeap As Long
'Dim lHeapAddress As Long
Private Sub SampleLoadSaveBuffer(szFileName As String)
Dim lHeapInitialSize As Long
Dim lMemInitialSize As Long
Dim nRet As Integer
'Create the heap
lHeap = 0
lHeapInitialSize = 1000
lHeap = HeapCreate(0, lHeapInitialSize, 0)
'Allocate the first chunk of memory to be 100 bytes
lHeapAddress = 0
lMemInitialSize = 1
lHeapAddress = HeapAlloc(lHeap, 0, lMemInitialSize)
LEAD1.Load szFileName, 0, 0, 1
LEAD1.SaveBufferSize= lMemInitialSize
LEAD1.SaveBufferAddress= lHeapAddress
LEAD1.EnableSaveBufferEvent= True
nRet = LEAD1.SaveBuffer(FILE_TIF, 24, 0, SAVE_OVERWRITE)
If (nRet <> 0) Then
MsgBox "SaveBuffer Error: " + Str(nRet)
End If
'Load the file in memory into a second LEAD control
nRet = LEAD1.LoadBuffer(LEAD1.SaveBufferAddress, 24, 0, 1, LEAD1.SaveBufferSize)
'Free the allocated memory
HeapFree lHeap, 0, lHeapAddress
'Destroy the heap
HeapDestroy lHeap
End Sub
'The SaveBuffer event allows the user to reallocate the buffer as needed
Private Sub LEAD1_SaveBuffer(ByVal uRequiredSize As Long)
Dim dRet As Long
lHeapAddress = HeapReAlloc(lHeap, 0, lHeapAddress, uRequiredSize)
If (lHeapAddress = 0) Then
MsgBox "HeapReAlloc Error"
LEAD1.EnableSaveBufferEvent= False
Else
LEAD1.SaveBufferSize = uRequiredSize
LEAD1.SaveBufferAddress = lHeapAddress
End If
End Sub