在VB中从Access中取出图片(OLE对象),并显示到PictureBox中,怎么做?
PictureBox控件: Picture1
ADODB.Recordset:rstStudent
字段:Fields("Picture")

解决方案 »

  1.   

    Public Sub WriteDB(Col As ADODB.Field, ImgFile As String, Optional BLOCKSIZE As Long = 8192)
        Dim byteData() As Byte, FileLength As Long, NumBlocks As Integer
        Dim LeftOver As Long, SourceFileNum As Integer, i As Integer
        
          SourceFileNum = FreeFile '打开的文件号 你可以直接写 例如: 1
          Open ImgFile For Binary As #SourceFileNum 'ImgFile是图片所在目录
            FileLength = LOF(SourceFileNum)
            If FileLength > 50 Then
              NumBlocks = FileLength / BLOCKSIZE
              LeftOver = FileLength Mod BLOCKSIZE
              ReDim byteData(LeftOver)
              Get #SourceFileNum, , byteData()
              Col.AppendChunk byteData()
              ReDim byteData(BLOCKSIZE)
              For i = 1 To NumBlocks
                Get #SourceFileNum, , byteData()
                Col.AppendChunk byteData()
              Next
            End If
          Close #SourceFileNum
    End Sub
    '读数据库Public Function ReadDB(Col As ADODB.Field, ImgFile As String, Optional BLOCKSIZE As Long = 8192) As Boolean
    Dim byteData() As Byte, NumBlocks As Integer
    Dim LeftOver As Long, DestFileNum As Integer, i As Integer
    Dim ColSize As LongOn Error GoTo ErrRead
      ReadDB = False  If Dir(ImgFile) = "tmpImage" Then Kill ImgFile  DestFileNum = FreeFile '同样是文件号 可以是 1
      Open ImgFile For Binary As #DestFileNum 'ImgFile读数据库后得到的图片文件名    ColSize = Col.ActualSize
        NumBlocks = ColSize / BLOCKSIZE
        LeftOver = ColSize Mod BLOCKSIZE    ReDim byteData(LeftOver)
        byteData() = Col.GetChunk(LeftOver)
        Put #DestFileNum, , byteData()
        ReDim byteData(BLOCKSIZE)
        For i = 1 To NumBlocks
          byteData() = Col.GetChunk(BLOCKSIZE)
          Put #DestFileNum, , byteData()
        Next
        If LOF(DestFileNum) > 200 Then ReadDB = True
      Close #DestFileNum
    Exit FunctionErrRead:
      MsgBox "读“图形”信息文件失败," & Err.Number, vbOKOnly + vbInformation
      ReadDB = False
      Exit Function
    End Function '//如果ReadDB=False则写文件失败。
      

  2.   

    调用   '显示图片
                   Dim Picture As Variant
                   Dim lngLogoSize As Long
                   Dim varChunk As Variant
                   lngLogoSize = rs!img.ActualSize
                   File_len = lngLogoSize
                   If File_len = 0 Then
                      Exit Sub
                   End If
                   varChunk = rs!img.GetChunk(lngLogoSize)
                   Picture = varChunk
                   'show picture in the ImgeditOcx
                   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
                    'get a temporary file name
                   File_name = TemportyFileName() '建立一个临时文件
                   
                   'open the file
                   File_num = FreeFile
                   Open File_name For Binary As #File_num
                   
                   'copy the data into the file
                   File_length = File_len
                   'file_length = picture.ActualSize
                   Num_blocks = File_length / BLOCK_SIZE
                   Left_over = File_length Mod BLOCK_SIZE
                   
                   For Block_num = 1 To Num_blocks
                   Bytes() = Picture '.GetChunk(BLOCK_SIZE)
                   Put #File_num, , Bytes()
                   Next Block_num
                   If Left_over > 0 Then
                   Bytes() = Picture '.GetChunk(left_over)
                   Put #File_num, , Bytes()
                   End If
                   Close #File_num
                   'display the picture file
                   .img.ClearDisplay
                   .img.Image = File_name
                   .img.Display
                   'Kill File_nam