大虾:
ADO不支持IMAGE的数据库存取。只有用DAO控件。在数据库中IMAGE的表中增加一个ID字段。要调出IAMGE时先找到ID,再用data.recordset.move or data.recordset.find 都可以定位到image,并显示出来。
ADO不支持IMAGE的数据库存取。只有用DAO控件。在数据库中IMAGE的表中增加一个ID字段。要调出IAMGE时先找到ID,再用data.recordset.move or data.recordset.find 都可以定位到image,并显示出来。
请问如何用代码将字段中的图片赋给IMAGE, 并显示出来? (定位不成问题)
比如:可以用LOADPICTURE函数将图片文件赋给IMAGE.
在dao下:
数据库中使用Long Binary字段放图片数据,在form中放一个Picture Box,放一个Data Control,把Picture和Data Control绑定。
储存图片:datacontrol.add new record; picture.loadpicture();datacontrol.update
读取图片则相反。如果不用上面的方法,你也可以用open binary file方式。
这个图片我是在ACCESS2000中放入的, 现在想在VB程序中显示出来.在程序中, 我不用控件而是用代码来定位记录, 因此, 必须用源码来显示图片到一个IMAGE控件中. ADO的appendchunk以及getchunk能存取图片,但我不知具体如何操作. 敬请给出示例源码.
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
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
'取图片
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,也懒得去深究,有知道的朋友告诉我一声。
Thank you very much. i've tried it out by your code.