大虾:
    ADO不支持IMAGE的数据库存取。只有用DAO控件。在数据库中IMAGE的表中增加一个ID字段。要调出IAMGE时先找到ID,再用data.recordset.move or data.recordset.find 都可以定位到image,并显示出来。

解决方案 »

  1.   

    yycx:
       请问如何用代码将字段中的图片赋给IMAGE, 并显示出来? (定位不成问题)
       比如:可以用LOADPICTURE函数将图片文件赋给IMAGE.
      

  2.   

    把图片直接储存到db中,然后再调出来可以用这样的办法实现。
    在dao下:
      数据库中使用Long Binary字段放图片数据,在form中放一个Picture Box,放一个Data Control,把Picture和Data Control绑定。
      储存图片:datacontrol.add new record; picture.loadpicture();datacontrol.update
      读取图片则相反。如果不用上面的方法,你也可以用open binary file方式。
      

  3.   

    不知道你的图片是用什么方式放进数据库的,用ADO当然可以存放和读取数据库中的图片。如果不用shyguy说的方法,用open binary file方式打开图片文件,用ADO的appendchunk把图片数据放入表中,用getchunk方法读取表中的图片数据,再显示到image中。如果还不清楚,可以找我要源码。
      

  4.   

    诸位大虾:
        这个图片我是在ACCESS2000中放入的, 现在想在VB程序中显示出来.在程序中, 我不用控件而是用代码来定位记录, 因此, 必须用源码来显示图片到一个IMAGE控件中. ADO的appendchunk以及getchunk能存取图片,但我不知具体如何操作. 敬请给出示例源码.
         
      

  5.   

        '取图片
        Dim lngSize as long
        dim abytPhoto() as byte
        lngSize = mrstPer("Graphics").ActualSize '获得该字段内容的真实长度 
        If lngSize > 0 Then
            ReDim abytPhoto(1 To lngSize)
            abytPhoto = mrstPer("Graphics").GetChunk(lngSize)
            strFile = GetTempFile   ‘GetTtmpFilr获得系统的临时目录,请换成自己的路径
            If Dir(strFile) <> "" Then Kill strFile
            lngFile = FreeFile
            Open strFile For Binary Access Write As #lngFile
            Put #lngFile, , abytPhoto
            Close #lngFile
            picPhoto.Picture = LoadPicture(strFile)
            Kill strFile
        Else
            picPhoto.Picture = LoadPicture("")
        End If
        用代码插入图片
        If picPhoto.Picture <> 0 Then   '如果图片存在
            strFile = GetTempFile    
            SavePicture picPhoto.Picture, strFile
            lngFile = FreeFile
            Open strFile For Binary Access Read As #lngFile
            lngFileLen = FileLen(strFile)
            If lngFileLen > 0 Then
                ReDim abytPhoto(1 To lngFileLen)
                Get #lngFile, , abytPhoto
                Close #lngFile
                rstItem!Graphics.AppendChunk abytPhoto
                rstItem.Update
            End If
        End If
      

  6.   

    如果从一个表中复制图片数据到另一个表,MSDN中有示例代码。
      

  7.   

    我以前问过这个问题,已经解决了的,要源码吗?其实firechun的是正确的
      

  8.   

      十分感谢firechun以及gx_sunny的答复。我采用了上述代码后,程序还是出错,号码481,提示无效图片,不知为何?有关代码如下:
         Set rstParameter = New ADODB.Recordset
         rstParameter.Open "select * from PrmGeneral", cnnParameter, adOpenStatic,  adLockOptimistic    ’仅一条记录    Dim lngSize As Long
        Dim abytPhoto() As Byte
        Dim strFile As String
        Dim lngFile As Integer
        
        lngSize = rstParameter("CCIC").ActualSize
        If lngSize > 0 Then
            ReDim abytPhoto(1 To lngSize)
            abytPhoto = rstParameter("CCIC").GetChunk(lngSize)
            strFile = App.Path & "RptIcon.bmp"
            If Dir(strFile) <> "" Then Kill strFile
            lngFile = FreeFile
            Open strFile For Binary Access Write As #lngFile
            Put #lngFile, , abytPhoto
            Close #lngFile
            Image1.Picture = LoadPicture(strFile)   ’错误481 无效图片
            Kill strFile
        Else
           Image1.Picture = LoadPicture("")
        End If
      
      

  9.   

    你用的ACCESS2000,刚才看了一下,ACCESS在刚BMP文件放入OLE对象时,加入了自己的一些信息,所以用上面的代码时会“图片无效”,因此读出图片数据时必须把这些信息去掉。多出来的信息一共是96字节,其中文件头加了74字节,文件尾加了22字节,用下面的代码就没问题了,我测试过。
        '取图片
        Dim lngSize As Long
        Dim abytPhoto() As Byte
        Dim lngFile         As Long
        Dim strFile         As String
        Dim abytSize()      As Byte
        Dim lngX            As Long
        
        lngSize = mrstPer("Photo").ActualSize '获得该字段内容的真实长度
        If lngSize > 0 Then
            ReDim abytPhoto(1 To lngSize)
            ReDim abytSize(1 To lngSize - 96)
            abytPhoto = mrstPer("Photo").GetChunk(lngSize)
            strFile = "c:\my documents\temp.bmp"
            For lngX = 74 To lngSize - 23
                abytSize(lngX - 73) = abytPhoto(lngX)
            Next
            If Dir(strFile) <> "" Then Kill strFile
            lngFile = FreeFile
            Open strFile For Binary Access Write As #lngFile
            Put #lngFile, , abytSize
            Close #lngFile
            picPhoto.Picture = LoadPicture(strFile)
            Kill strFile
        Else
            picPhoto.Picture = LoadPicture("")
        End If
    我没用过VB+ACCESS编程,如果有更好的方法,请告知。
    另外,刚才用代码往ACCESS里插图片,居然没有成功。(用ADDNEW方法和UPDATE),也没有任何错误提示,不知道怎么回事。因为没用过ACCESS,也懒得去深究,有知道的朋友告诉我一声。
      

  10.   

    firechun:
       Thank you very much. i've tried it out by your code.