VB中保存图片问题
如果要把Text1中的文字和Picture1中的图片保存在同一个文件中,且能打开并读取该文件中的文字和图片到Text1和Picture1中,代码应该怎么写?...
如果要把Text1中的文字和Picture1中的图片保存在同一个文件中,且能打开并读取该文件中的文字和图片到Text1和Picture1中,代码应该怎么写?
展开
2个回答
展开全部
这需要你有VB+数据库开发的相关知识,首先你得建一个数据库文件(如ACCESS或SQL SERVER 2000),再建一个表如TMP_TABLE,再在表中建两个字段,一个是文件类型,一个是二进制类型。通过如下代码就可能将Text1中的文字和Picture1中的图片保存在同一个数据库文件中。下面是一个保存图片的示例代码,可供参考:
dim aduSave as New ADODB.Recordset
aduSave.Open " select Picture from tblBUltrasonicCheckPic where PersonID='" & txtInfo(0) & "' and ExamineID='" & strExamID & "' and PictureID=" & I, conn, adOpenDynamic, adLockBatchOptimistic
DoEvents
saveCPic(aduSave.Fields("Picture"))
aduSave.UpdateBatch
Public Sub SavePic(adrPic As ADODB.field)
'--保存图像
On Error Resume Next
'On Error GoTo err
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
file_num = FreeFile
Open "c:\photo_pic.jpg" 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
ReDim bytes(BLOCK_SIZE)
For block_num = 1 To num_blocks
Get #file_num, , bytes()
adrPic.AppendChunk bytes()
Next block_num
If left_over > 0 Then
ReDim bytes(left_over)
Get #file_num, , bytes()
adrPic.AppendChunk bytes()
End If
Close #file_num
End If
DoEvents: DoEvents: DoEvents
On Error Resume Next
Kill "c:\photo_pic.jpg"
DoEvents
Exit Sub
err:
Debug.Print err.Description
End Sub
Public Sub showPic(ByVal Pic As ADODB.field)
'--显示图像用
On Error Resume Next
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
Dim hgt As Single
DoEvents
file_num = FreeFile
Kill "c:\photo_pic.jpg"
DoEvents
Open "c:\photo_pic.jpg" For Binary As #file_num
file_length = Pic.ActualSize
num_blocks = file_length / BLOCK_SIZE
left_over = file_length Mod BLOCK_SIZE
For block_num = 1 To num_blocks
bytes() = Pic.GetChunk(BLOCK_SIZE)
Put #file_num, , bytes()
Next block_num
If left_over > 0 Then
bytes() = Pic.GetChunk(left_over)
Put #file_num, , bytes()
End If
Close #file_num
End Sub
dim aduSave as New ADODB.Recordset
aduSave.Open " select Picture from tblBUltrasonicCheckPic where PersonID='" & txtInfo(0) & "' and ExamineID='" & strExamID & "' and PictureID=" & I, conn, adOpenDynamic, adLockBatchOptimistic
DoEvents
saveCPic(aduSave.Fields("Picture"))
aduSave.UpdateBatch
Public Sub SavePic(adrPic As ADODB.field)
'--保存图像
On Error Resume Next
'On Error GoTo err
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
file_num = FreeFile
Open "c:\photo_pic.jpg" 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
ReDim bytes(BLOCK_SIZE)
For block_num = 1 To num_blocks
Get #file_num, , bytes()
adrPic.AppendChunk bytes()
Next block_num
If left_over > 0 Then
ReDim bytes(left_over)
Get #file_num, , bytes()
adrPic.AppendChunk bytes()
End If
Close #file_num
End If
DoEvents: DoEvents: DoEvents
On Error Resume Next
Kill "c:\photo_pic.jpg"
DoEvents
Exit Sub
err:
Debug.Print err.Description
End Sub
Public Sub showPic(ByVal Pic As ADODB.field)
'--显示图像用
On Error Resume Next
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
Dim hgt As Single
DoEvents
file_num = FreeFile
Kill "c:\photo_pic.jpg"
DoEvents
Open "c:\photo_pic.jpg" For Binary As #file_num
file_length = Pic.ActualSize
num_blocks = file_length / BLOCK_SIZE
left_over = file_length Mod BLOCK_SIZE
For block_num = 1 To num_blocks
bytes() = Pic.GetChunk(BLOCK_SIZE)
Put #file_num, , bytes()
Next block_num
If left_over > 0 Then
bytes() = Pic.GetChunk(left_over)
Put #file_num, , bytes()
End If
Close #file_num
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询