'....略去记录集打开过程 '图片文件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 '....
'设置临时照片文件 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
'图片文件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
'....
才能得到你要想的效果了.
'设置临时照片文件
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
Image1.Picture = LoadPicture("c:\t.jpg")
http://www.dapha.net/down/list.asp?id=1826