how to upload photo in visual basic

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • slapshock
    New Member
    • Oct 2006
    • 57

    how to upload photo in visual basic

    gud morning!!!!!!!!

    i have a problem on how to upload photo and save it in the database....
    i am not so familiar in visual basic on how to code.....
    can you help me with this problem??
    i want the codes that can the uploading and saving photos in visual basics....




    tnx
    slapshock
  • Hemant Pathak
    Recognized Expert New Member
    • Jul 2006
    • 92

    #2
    To add a new record, the program lets the user select a graphic file. The program opens the file and reads it in blocks of binary data. For each block, the program uses AppendChunk to add the block to the database

    Code:
     
    Private Sub mnuRecordAdd_Click()
    Dim rs As ADODB.Recordset
    Dim person_name As String
    Dim file_num As String
    Dim file_length As String
    Dim bytes() As Byte
    Dim num_blocks As Long
    Dim left_over As Long
    Dim block_num As Long
    
        person_name = InputBox("Name")
        If Len(person_name) = 0 Then Exit Sub
    
        dlgPicture.Flags = _
            cdlOFNFileMustExist Or _
            cdlOFNHideReadOnly Or _
            cdlOFNExplorer
        dlgPicture.CancelError = True
        dlgPicture.Filter = "Graphics " & _
            "Files|*.bmp;*.ico;*.jpg;*.gif"
    
        On Error Resume Next
        dlgPicture.ShowOpen
        If Err.Number = cdlCancel Then
            Exit Sub
        ElseIf Err.Number <> 0 Then
            MsgBox "Error " & Format$(Err.Number) & _
                " selecting file." & vbCrLf & Err.Description
            Exit Sub
        End If
    
        ' Open the picture file.
        file_num = FreeFile
        Open dlgPicture.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
    
            Set rs = New ADODB.Recordset
            rs.CursorType = adOpenKeyset
            rs.LockType = adLockOptimistic
            rs.Open "Select Name, Picture, FileLength FROM " & _
                "People", m_DBConn
    
            rs.AddNew
            rs!Name = person_name
            rs!FileLength = file_length
    
            ReDim bytes(BLOCK_SIZE)
            For block_num = 1 To num_blocks
                Get #file_num, , bytes()
                rs!Picture.AppendChunk bytes()
            Next block_num
    
            If left_over > 0 Then
                ReDim bytes(left_over)
                Get #file_num, , bytes()
                rs!Picture.AppendChunk bytes()
            End If
    
            rs.Update
            Close #file_num
    
            lstPeople.AddItem person_name
            lstPeople.Text = person_name
        End If
    End Sub

    To retreive a picture, the program selects the appropriate database record. It uses the TemporaryFileNa me function to get a temporary file name and opens the file. It then uses GetChunk to read the data from the database in blocks and writes the blocks into the file. The program then uses LoadPicture to load the file, and Kill to remove it.

    Code:
    ' Display the clicked person.
    Private Sub lstPeople_Click()
    Dim rs As ADODB.Recordset
    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
    
        picPerson.Visible = False
        Screen.MousePointer = vbHourglass
        DoEvents
    
        ' Get the record.
        Set rs = m_DBConn.Execute("SELECT * FROM People WHERE " & _
            "Name='" & _
            lstPeople.Text & "'", , adCmdText)
        If rs.EOF Then Exit Sub
    
        ' Get a temporary file name.
        file_name = TemporaryFileName()
    
        ' Open the file.
        file_num = FreeFile
        Open file_name For Binary As #file_num
    
        ' Copy the data into the file.
        file_length = rs!FileLength
        num_blocks = file_length / BLOCK_SIZE
        left_over = file_length Mod BLOCK_SIZE
    
        For block_num = 1 To num_blocks
            bytes() = rs!Picture.GetChunk(BLOCK_SIZE)
            Put #file_num, , bytes()
        Next block_num
    
        If left_over > 0 Then
            bytes() = rs!Picture.GetChunk(left_over)
            Put #file_num, , bytes()
        End If
    
        Close #file_num
    
        ' Display the picture file.
        picPerson.Picture = LoadPicture(file_name)
        picPerson.Visible = True
    
        Width = picPerson.Left + picPerson.Width + Width - _
            ScaleWidth
        hgt = picPerson.Top + picPerson.Height + Height - _
            ScaleHeight
        If hgt < 1440 Then hgt = 1440
        Height = hgt
    
        Kill file_name
        Screen.MousePointer = vbDefault
    End Sub
    
    ' Return a temporary file name.
    Private 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 = Space$(MAX_PATH)
        length = GetTempPath(MAX_PATH, temp_path)
        temp_path = Left$(temp_path, length)
    
        ' Get the file name.
        temp_file = Space$(MAX_PATH)
        GetTempFileName temp_path, "per", 0, temp_file
        TemporaryFileName = Left$(temp_file, InStr(temp_file, _
            Chr$(0)) - 1)
    End Function
    Last edited by debasisdas; Feb 16 '09, 07:36 AM. Reason: added code tags

    Comment

    • vinci
      New Member
      • Aug 2006
      • 62

      #3
      thanks a lot! it will really help me a lot...
      salamat!!! - slapshock

      Comment

      • Rowelle
        New Member
        • Feb 2009
        • 1

        #4
        how to upload photo in vb6

        help me guys how to upload photo in vb6..please i nid in my thesis if you have a sample program please send to my email here my email mail id removed thanks to all..godbless
        Last edited by debasisdas; Feb 16 '09, 07:34 AM. Reason: removed mail id

        Comment

        • debasisdas
          Recognized Expert Expert
          • Dec 2006
          • 8119

          #5
          Originally posted by Rowelle
          help me guys how to upload photo in vb6..please i nid in my thesis if you have a sample program please send to my email here my email mail id removed thanks to all..godbless
          have you tried the code posted in this thread ?

          Comment

          Working...