在VB中,如何用Access保存图片并在程序中显示呢?最好能给出例子!急用,请发信至[email protected]

解决方案 »

  1.   

    '....略去记录集打开过程
    '图片文件t.jpg存入数据库Img字段
    Dim bit() As Byte, fn As Byte, Chunk() As Byte
    fn = FreeFile()
    Open ("c:\t.jpg") For Binary As fn
    ReDim bit(LOF(1)) As Byte
    Get fn, 1, bit
    Close fn
    Rs("Img").AppendChunk bit
    Rs.Update
    '....
    '取出图片,形成文件t.jpg
    If Not IsNull(Rs("Img")) Then
        'Chunk() = Rs("Img").GetChunk(Rs("Img").ActualSize)  'SQL Server
        Chunk() = Rs("Img").GetChunk(0,Rs("Img").FieldSize)  'Access 
    End If
    If Dir("c:\t.jpg") <> "" Then Kill "c:\t.jpg"
    fn = FreeFile
    Open "c:\t.jpg" For Binary Access Write As fn
    Put fn, , Chunk()
    Close fn
    '....
      

  2.   

    你要使用ADO2.7.
    才能得到你要想的效果了.
      

  3.   

    请问,zsgzsgzsg(zsg),那个img字段是什么类型的呢?  OLE的吗? 用这个方法不能在image的控件中显示啊,这是为什么呢?
      

  4.   


    '设置临时照片文件
    Public Function ReadDB(col As ADODB.Field, ByRef imgFile As String) As Boolean
        On Error GoTo ErrRead
        Dim mStream As New ADODB.Stream
        ReadDB = False
        
        If col.ActualSize < 200 Then Exit Function
        
        mStream.Type = adTypeBinary
        mStream.Open
        mStream.Write col.Value
        mStream.SaveToFile imgFile, adSaveCreateOverWrite
        ReadDB = True
        Exit Function
    ErrRead:
         MsgBox "设置临时照片文件时出现错误:" & vbCrLf & Err.Description, vbInformation, "提示"
         ReadDB = False
    End Function然后:Dim strPath As String
    dim rsPhoto as new adodb.recordset
    rsphoto.open "select photo from tableName",con,..'photo 为OLE字段,存储照片    '处理照片
        If rsPhoto.RecordCount >= 1 Then '有照片
            strPath = App.Path & "\tempPhoto"
            If rsPhoto(0).ActualSize > 200 Then
                If ReadDB(rsPhoto(0), strPath) Then
                    Set picPhoto.Picture = LoadPicture(strPath)‘picPhoto为PictureBox
                End If
            End If
        end if
      

  5.   

    http://www.dapha.net/down/list.asp?id=1826这儿有样例下载(源码)
      

  6.   

    img字段是OLE类型。  
    Image1.Picture = LoadPicture("c:\t.jpg")
      

  7.   

    将图片写入数据库并显示
    http://www.dapha.net/down/list.asp?id=1826