VB 显示 ACCESS 数据库的里的图片
我在ACCESS数据库里增加了一个图片的字段,在数据库里显示“包”双击的时候就可以显示图片,我现在在VB里增加了一个DataGrid控件,可以显示出数据库的内容,但是我图...
我在ACCESS 数据库里增加了一个图片的字段,在数据库里显示“包” 双击的时候就可以显示图片,
我现在在VB里 增加了一个 DataGrid 控件 ,可以显示出 数据库的内容 , 但是我图片的字段就是乱码了,我想再 在窗体上增加个 Image1 类的控件 以此显示 出数据库的 图片 ,但是 一直没弄好。。所以 请教做过的朋友 帮我下。。
下面是我用的控件
Adodc1 DataGrid1 Image1。 展开
我现在在VB里 增加了一个 DataGrid 控件 ,可以显示出 数据库的内容 , 但是我图片的字段就是乱码了,我想再 在窗体上增加个 Image1 类的控件 以此显示 出数据库的 图片 ,但是 一直没弄好。。所以 请教做过的朋友 帮我下。。
下面是我用的控件
Adodc1 DataGrid1 Image1。 展开
2个回答
展开全部
''这是调用文宽如带件,你自己改一改吧,图片存数据库是二慎芦进制型式
Public Function TemporaryFileName() As String '生成临时文件
Dim temp_path As String
Dim temp_file As String
Dim length As Long
temp_path = Space$(MAX_PATH)
length = GetTempPath(MAX_PATH, temp_path)
temp_path = Left$(temp_path, length)
temp_file = Space$(MAX_PATH)
GetTempFileName temp_path, "per", 0, temp_file
TemporaryFileName = Left$(temp_file, InStr(temp_file, Chr$(0)) - 1)
End Function
Public Sub DispPic(ByVal Person_no As String, ByVal PicObject As Object)
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
Screen.MousePointer = vbHourglass
DoEvents
'数据库自己改吧
Set rs = cn.Execute("SELECT * FROM ep_Picture WHERE e_no='" & _
Person_no & "'", , adCmdText)
If rs.EOF Then
Screen.MousePointer = vbDefault
Exit Sub
End If
'生成文件名
file_name = TemporaryFileName()
'打开临时文件写入数据橡并
file_num = FreeFile
Open file_name For Binary As #file_num
file_length = rs!FileLength
num_blocks = file_length / BLOCK_SIZE
left_over = file_length Mod BLOCK_SIZE
For block_num = 1 To num_blocks
bytes() = rs!Picture.GetChunk(BLOCK_SIZE)
Put #file_num, , bytes()
Next block_num
If left_over > 0 Then
bytes() = rs!Picture.GetChunk(left_over)
Put #file_num, , bytes()
End If
Close #file_num
'返回图片控件
PicObject.Picture = LoadPicture(file_name)
Kill file_name
Screen.MousePointer = vbDefault
End Sub
Public Sub AddPic(ByVal person_name As String, ByVal FileName As String)
Dim file_num As String
Dim file_length As String
Dim bytes() As Byte
Dim num_blocks As Long
Dim left_over As Long
Dim block_num As Long
If Len(person_name) = 0 Then Exit Sub
file_num = FreeFile
Open FileName For Binary Access Read As #file_num
file_length = LOF(file_num)
If file_length > 0 Then
num_blocks = file_length / BLOCK_SIZE
left_over = file_length Mod BLOCK_SIZE
Set rs = New ADODB.Recordset
rs.CursorType = adOpenKeyset
rs.LockType = adLockOptimistic
rs.Open "Select e_no, Picture, FileLength FROM ep_picture", cn
rs.AddNew
rs!e_no = person_name
rs!FileLength = file_length
ReDim bytes(BLOCK_SIZE)
For block_num = 1 To num_blocks
Get #file_num, , bytes()
rs!Picture.AppendChunk bytes()
Next block_num
If left_over > 0 Then
ReDim bytes(left_over)
Get #file_num, , bytes()
rs!Picture.AppendChunk bytes()
End If
rs.Update
Close #file_num
End If
End Sub
Public Function TemporaryFileName() As String '生成临时文件
Dim temp_path As String
Dim temp_file As String
Dim length As Long
temp_path = Space$(MAX_PATH)
length = GetTempPath(MAX_PATH, temp_path)
temp_path = Left$(temp_path, length)
temp_file = Space$(MAX_PATH)
GetTempFileName temp_path, "per", 0, temp_file
TemporaryFileName = Left$(temp_file, InStr(temp_file, Chr$(0)) - 1)
End Function
Public Sub DispPic(ByVal Person_no As String, ByVal PicObject As Object)
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
Screen.MousePointer = vbHourglass
DoEvents
'数据库自己改吧
Set rs = cn.Execute("SELECT * FROM ep_Picture WHERE e_no='" & _
Person_no & "'", , adCmdText)
If rs.EOF Then
Screen.MousePointer = vbDefault
Exit Sub
End If
'生成文件名
file_name = TemporaryFileName()
'打开临时文件写入数据橡并
file_num = FreeFile
Open file_name For Binary As #file_num
file_length = rs!FileLength
num_blocks = file_length / BLOCK_SIZE
left_over = file_length Mod BLOCK_SIZE
For block_num = 1 To num_blocks
bytes() = rs!Picture.GetChunk(BLOCK_SIZE)
Put #file_num, , bytes()
Next block_num
If left_over > 0 Then
bytes() = rs!Picture.GetChunk(left_over)
Put #file_num, , bytes()
End If
Close #file_num
'返回图片控件
PicObject.Picture = LoadPicture(file_name)
Kill file_name
Screen.MousePointer = vbDefault
End Sub
Public Sub AddPic(ByVal person_name As String, ByVal FileName As String)
Dim file_num As String
Dim file_length As String
Dim bytes() As Byte
Dim num_blocks As Long
Dim left_over As Long
Dim block_num As Long
If Len(person_name) = 0 Then Exit Sub
file_num = FreeFile
Open FileName For Binary Access Read As #file_num
file_length = LOF(file_num)
If file_length > 0 Then
num_blocks = file_length / BLOCK_SIZE
left_over = file_length Mod BLOCK_SIZE
Set rs = New ADODB.Recordset
rs.CursorType = adOpenKeyset
rs.LockType = adLockOptimistic
rs.Open "Select e_no, Picture, FileLength FROM ep_picture", cn
rs.AddNew
rs!e_no = person_name
rs!FileLength = file_length
ReDim bytes(BLOCK_SIZE)
For block_num = 1 To num_blocks
Get #file_num, , bytes()
rs!Picture.AppendChunk bytes()
Next block_num
If left_over > 0 Then
ReDim bytes(left_over)
Get #file_num, , bytes()
rs!Picture.AppendChunk bytes()
End If
rs.Update
Close #file_num
End If
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-01-09
展开全部
去VBhao 看看 有相应的 开源程序。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |