Image Handling
This article describes about the storing photos into a database and retrive the stored photos from a database.
Image Handling in VB
This article describes about the storing photos into a database and retrieve the stored photos from a database.
Create a form and place the following controls with their properties
This command button opens a dialog box and the user can select any image or photo
command button
name : CmdLoadPhoto
caption: select photo
This command button inserts the displaying photo into the corresponding database
command button
name : cmdInsertPhoto
caption: Insert photo into database
This command button clears the screen
command button
name : cmdClearFields
caption: clear the screen
This command button retrieve the photo from the database which one is indicated or specified
command button
name : cmdRetrievePicture
caption: Retrive photo from database
This picture box just displays the photo
picture box
name: picture1
In code window, you have to write the following codes for the above mentioned command buttons using thier names
Option Explicit
Const BLOCK_SIZE As Long = 100000 'bytes
Dim Fld_No As String
Dim fileSize As Long
Dim FileName As String
Private Sub CmdLoadPhoto_Click()
Dim x As String
If CmdAdd.Caption = "&Save" Then
CDP.FileName = ""
CDP.ShowOpen
If CDP.FileName <> "" Then
x = CDP.FileName
FileName = x
Picture1.Picture = LoadPicture(x)
End If
ElseIf CmdMod.Caption = "&Update" Then
CDP.FileName = ""
CDP.ShowOpen
If CDP.FileName <> "" Then
x = CDP.FileName
FileName = x
Picture1.Picture = LoadPicture(x)
End If
End If
End Sub
Private Sub cmdInsertPhoto_click()
Dim Qry As String
Dim sourceFile As Integer
sourceFile = FreeFile
Open FileName For Binary Access Read As sourceFile
fileSize = LOF(sourceFile)
If fileSize = 0 Then
Close sourceFile
MsgBox "Employee's Photo is invalid"
Exit Sub
Else
Dim pictBlocks As Integer
pictBlocks = fileSize / BLOCK_SIZE
Dim leftOverData As Long
leftOverData = fileSize Mod BLOCK_SIZE
Dim pictData() As Byte
ReDim pictData(leftOverData)
Get sourceFile, , pictData()
rs.Open "select * from table where id =1", CnC, adOpenDynamic, adLockOptimistic
rs("Photo").AppendChunk pictData()
ReDim pictData(BLOCK_SIZE)
Dim i As Integer
For i = 1 To pictBlocks
Get sourceFile, , pictData()
rs("Photo").AppendChunk pictData()
Next i
rs("PhotoSize") = fileSize
rs.Update
Close sourceFile
End If
Me.MousePointer = vbNormal
ClearFields
End Sub
Private Sub cmdClearFields_click()
Dim con As Control
For Each con In Controls
If TypeOf con Is Image Then
con.Picture = Nothing
End If
Next
End Sub
Private Sub cmdRetrievePicture_click()
Dim K As String
Dim Ga As New ClsCommon
Dim Xval As Variant
Dim diskFile As String
diskFile = App.Path & "\ajackson.bmp"
' Delete the temp picture file.
If Len(Dir$(diskFile)) > 0 Then
Kill diskFile
End If
Xval = Ga.G_CodeToName(CnC, "It_Code", Trim(TxtItemCode), "PhotoSize", "itemmast", "")
fileSize = Xval
'Get a free file handle
Dim destfileNum As Long
destfileNum = FreeFile
'Open the file
Open diskFile For Binary As destfileNum
'Calculate the number of blocks (100000 bytes blocks)
Dim pictBlocks As Integer
pictBlocks = fileSize / BLOCK_SIZE
'Calculate the left over data
Dim leftOverData As Long
leftOverData = fileSize Mod BLOCK_SIZE
'Byte array for Picture data.
Dim pictData() As Byte
'Get the left over data first
pictData() = rs("Photo").GetChunk(leftOverData)
'write the binary picture data from a variable to disk file
Put destfileNum, , pictData()
Dim i
'Now get the remaining binary picture data in Blocks of 100000
For i = 1 To pictBlocks
'pictData() = RsEmp("Photo").GetChunk(BLOCK_SIZE)
pictData() = rs("Photo").GetChunk(BLOCK_SIZE)
Put destfileNum, , pictData()
Next i
'Close the file handle
Close destfileNum
'Load the temp Picture into the Image control
Picture1.Picture = LoadPicture(App.Path & "\ajackson.bmp")
End Sub