© Microsoft Corporation. All rights reserved.

VB5, VB6 Retrieve an Image From the Database
Public Sub FillFields()
'Propagate fields with record data

Dim lngImageSize As Long
Dim lngOffset As Long
Dim bytChunk() As Byte
Dim intFile As Integer
Dim strTempPic As String
Const conChunkSize = 100
Dim strImage As String

If Not (rs.BOF And rs.EOF) Then
   txtImageTitle.Text = rs.Fields("ImageTitle")
		
   If Trim$("" & rs.Fields("ImagePath")) = "" Then 
      'Image saved as a BLOB
      txtImagePath.Text = ""
      optImageType(1).Value = True

      'Make sure the temporary file 
      'doesn't exist already 
      strTempPic = App.Path & "\TempPic.jpg"
      If Len(Dir(strTempPic)) > 0 Then
         Kill strTempPic
      End If

      'Open the temporary file to save the BLOB to
      intFile = FreeFile
      Open strTempPic For Binary As #intFile

      'Read the binary data into 
      'the byte variable array
      lngImageSize = rs("ImageBLOB").ActualSize
      Do While lngOffset < lngImageSize
         bytChunk() = rs _
            ("ImageBLOB").GetChunk(conChunkSize)
         Put #intFile, , bytChunk()
         lngOffset = lngOffset + conChunkSize
      Loop
      Close #intFile

      'After loading the image, get 
      'rid of the temporary file
      imgDBImage.Picture = LoadPicture(strTempPic)
      Kill strTempPic
   Else 'Image saved as file pointer
      txtImagePath.Text = rs.Fields("ImagePath")
      optImageType(0).Value = True
      If Not IsNull(rs.Fields ("ImagePath")) Then
         strImage = Trim$("" & rs.Fields("ImagePath"))
         If Len(Dir(strImage)) Then
            imgDBImage.Picture = LoadPicture(strImage)
         Else
            imgDBImage.Picture = LoadPicture()
         End If
      End If
   End If
End If
End Sub
Listing 1 | Retrieve a record from the database and propagate the fields on the form. If the image path is empty, the program assumes the image is stored as a BLOB. The procedure assumes you've opened the recordset (rs) and positioned it to the correct record.
Page view tracker