I ever made an application for a company when I work at migent computer an accounting software house in Jakarta, Indonesia. A part of that application need to save or load a picture. I don’t have any idea at time how to save/load a picture from a database. Thanks God, my senior there has the code. But, the code I got from my senior has a problem with loading time. At time I guess that Image Loading Time Problem (ILTP) cause of the code set data source and data field for images control. But I ignore it at time because of deadline. And then when I am a freelance programmer I found the same case again. I need to solve time loading. Then I try to find it at the home millions of lines of source code. Thanks God I find what I’m looking for. A class from Khalid Mahmod does help me.
‘----------- begin class (clsPicture.cls) ---------
‘ Class source code came from Planet-source-code
‘ Original Author: Khalid Mahmood, Lahore, Pakistan (k_mahmod@hotmail.com)
Private Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Public Sub LoadPhoto(rstMain As Recordset, PFName As String, SizeField As String, picEmp As Image)
On Error GoTo Handler
Dim bytes() As Byte
Dim file_name As String
Dim file_num As Integer
Dim file_length As Long
Dim num_blocks As Long
Dim left_over As Long
Dim block_num As Long
Dim hgt As Single
Screen.MousePointer = vbHourglass
DoEvents
file_name = TemporaryFileName()
file_num = FreeFile
Open file_name For Binary As #file_num
file_length = rstMain(SizeField)
num_blocks = file_length / BLOCK_SIZE
left_over = file_length Mod BLOCK_SIZE
For block_num = 1 To num_blocks
bytes() = rstMain(PFName).GetChunk(BLOCK_SIZE)
Put #file_num, , bytes()
Next block_num
If left_over > 0 Then
bytes() = rstMain(PFName).GetChunk(left_over)
Put #file_num, , bytes()
End If
Close #file_num
picEmp.Picture = LoadPicture(file_name)
Screen.MousePointer = vbDefault
Exit Sub
Handler:
Resume Next
End Sub
Public Sub SavePhoto(filename As String, rstMain As Recordset, FieldName As String, SizeField As String)
On Error GoTo Handler
Dim file_num As String
Dim file_length As Long
Dim bytes() As Byte
Dim num_blocks As Long
Dim left_over As Long
Dim block_num As Long
file_num = FreeFile
Open filename For Binary Access Read As #file_num
file_length = LOF(file_num)
If file_length > 0 Then
num_blocks = file_length / BLOCK_SIZE
left_over = file_length Mod BLOCK_SIZE
rstMain(SizeField) = file_length
ReDim bytes(BLOCK_SIZE)
For block_num = 1 To num_blocks
Get #file_num, , bytes()
rstMain(FieldName).AppendChunk bytes()
Next block_num
If left_over > 0 Then
ReDim bytes(left_over)
Get #file_num, , bytes()
rstMain(FieldName).AppendChunk bytes()
End If
'rstEmployee.Update
Close #file_num
End If
Exit Sub
Handler:
MsgBox Err.Description
End Sub
Public Function TemporaryFileName() As String
Dim temp_path As String
Dim temp_file As String
Dim length As Long
' Get the temporary file path.
temp_path = VBA.Space$(MAX_PATH)
length = GetTempPath(MAX_PATH, temp_path)
temp_path = Left$(temp_path, length)
' Get the file name.
temp_file = VBA.Space$(MAX_PATH)
GetTempFileName temp_path, "per", 0, temp_file
TemporaryFileName = Left$(temp_file, InStr(temp_file, VBA.Chr$(0)) - 1)
End Function
‘----------- end of class (clsPicture.cls) --------
I add codes below in Form Where i want to Load/Save a Picture. In this case i had one Image control named Foto. And i’ll add or remove picture by right click at image control, so i make menu from menu editor:
mnuFoto -> visible False
--- mnuAddFoto
--- mnuRemoveFoto
And to fill blank picture I give an image put in Vb Resource with who-am-I as Id. If you don’t want to just remark all code contain LoadResPicture
‘------------------------------------------------------------------------
Dim FullPathFoto As String, RemoveFoto As Boolean
Private Sub Foto_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then PopupMenu mnuFoto
End Sub
Private Sub mnuAddFoto_Click()
FullPathFoto = ""
FullPathFoto = ShowOpenPictures(Me.hwnd, False, Func.CekPath(App.Path) & "Foto")
If FullPathFoto <> "" Then Me.Foto.Picture = LoadPicture(FullPathFoto) Else Foto.Picture = LoadResPicture("who-am-i", vbResBitmap)
End Sub
Private Sub mnuRemoveFoto_Click()
FullPathFoto = "": RemoveFoto = True
Foto.Picture = LoadResPicture("who-am-i", vbResBitmap)
End Sub
‘--------------------------------------------
‘ add this code in your save (add new) procedure
‘-------------------------------------------
Dim PIC As New clsPicture
If FullPathFoto <> "" Then
PIC.SavePhoto FullPathFoto, Rst, "Foto", "FotoSize"
Else
Rst!Foto = Null
Rst!FotoSize = Null
End If
Set PIC = Nothing
‘----------------------------------------------
‘ add this code in your edit procedure
‘-----------------------------------------------
Dim PIC As New clsPicture
If FullPathFoto <> "" Then
PIC.SavePhoto FullPathFoto, Rst, "Foto", "FotoSize"
Else
If RemoveFoto Then
Rst!Foto = Null
Rst!FotoSize = Null
End If
End If
Set PIC = Nothing
‘--------------------------------------------------
‘ add this code into your recordset loading
‘--------------------------------------------------
If Not IsNull(Rst!Foto) Then
Dim PIC As New clsPicture
PIC.LoadPhoto Rst, "Foto", "FotoSize", Foto
Set PIC = Nothing
Else
Foto.Picture = LoadResPicture("who-am-i", vbResBitmap)
End If
‘For MIGENT Only
‘Delete code below at FillForm() procedure and add codes above:
Set Foto.DataSource = record_set
Foto.DataField = field_name
‘-----------------------------------------
‘ Special for MIGENT Computer J add this in your xEdit() or ClearForm() Procedure
‘------------------------------------------
FullPathFoto = "": RemoveFoto = False
Foto.Picture = LoadResPicture("who-am-i", vbResBitmap)
'------- end of code ----------
By implement code above, I can say good bye to ILTP.