[求助][讨论]图片保存成为.dat文件的方法
VB入门刚开始学VB(大半年),老师要求做大作业,然后有个地方不太明白。
就是,老师要求不用数据库写通讯录
其他地方我都解决了
就差将图片转为数据(.dat)储存,然后再转为图片显示
这样说也许有写不明白
如图:
上传图片已经没问题啦,就差保存
这只是第二个界面,我想只显示这个应该足够了
希望各位高手给点提示我(再次强调,不能使用数据库)
代码如下:
===============================================
'定义显示
Public Sub showdata()
Get #1, current, student '公共事件模块,current,student已经定义为变量string
'Image1.Picture = student.photo (不敢肯定是不是语句错,运行不了)
Text1(0).Text = Trim(student.no)
Text1(1).Text = Trim(student.xingming)
Text1(2).Text = Trim(student.sex)
Text1(3).Text = Trim(student.thone)
Text1(4).Text = Trim(student.qq)
Text1(5).Text = Trim(student.homethone)
Text1(6).Text = Trim(student.address)
End Sub
'定义存储
Public Sub savedata()
'student.photo = Trim(Image1.Picture)
student.no = Trim(Text1(0).Text)
student.xingming = Trim(Text1(1).Text)
student.sex = Trim(Text1(2).Text)
student.thone = Trim(Text1(3).Text)
student.qq = Trim(Text1(4).Text)
student.homethone = Trim(Text1(5).Text)
student.address = Trim(Text1(6).Text)
Put #1, current, student
End Sub
'窗體運行
Private Sub Form_Load()
'窗體居中
Form1.Top = (Screen.Height - Form1.Height) / 2
Form1.Left = (Screen.Width - Form1.Width) / 2
'載入圖片
Picture1.Picture = LoadPicture(App.Path + "/编辑栏.jpg")
last = filelen(myfile) / Len(student) ’myfile为.dat的文件名
End Sub
'讀取數據
Private Sub Form_Activate()
If (r <= last) And (r > 0) Then
current = r
Else
current = 1
End If
Call showdata
For i = 0 To 6
Text1(i).Locked = True
Next i
End Sub
'上传图片按钮
Private Sub Compicture_Click() 'compicture为上传按钮
Dim Path_Picture As String
On Error Resume Next
CD1.CancelError = True
CD1.DialogTitle = "打开照片"
CD1.FileName = App.Path
CD1.DefaultExt = "JPG图像(.jpg)|*.jpg" '最先显示
CD1.Filter = "bmp图像(.bmp)|*.bmp|JPEG图像(.JPG)|*.JPG|GIF图像(.GIF)|*.GIF|所有文件|*.*"
CD1.ShowOpen
If Err = cdlCancel Then
Exit Sub
End If
Path_Picture = CD1.FileName
Image1.Picture = LoadPicture(Path_Picture)
ChangePicture = True
End Sub
'上一條
Private Sub Command1_Click()
Dim i As Integer
For i = 0 To 6
Text1(i).Locked = True
Next i
If current = 1 Then
MsgBox "你所瀏覽的已經是第一條數據!", vbInformation, "提示!"
Else
Call savedata
current = current - 1
Call showdata
End If
End Sub
'下一條
Private Sub Command2_Click()
Dim i As Integer
For i = 0 To 6
Text1(i).Locked = True
Next i
If current = last Then
MsgBox "你所瀏覽的已經是最后一條數據!", vbInformation, "提示!"
Else
Call savedata
current = current + 1
Call showdata
End If
End Sub
'第一條
Private Sub Command3_Click()
Dim i As Integer
For i = 0 To 6
Text1(i).Locked = True
Next i
Call savedata
current = 1
Call showdata
End Sub
'最後一條
Private Sub Command4_Click()
Dim i As Integer
For i = 0 To 6
Text1(i).Locked = True
Next i
Call savedata
current = last
Call showdata
End Sub
'編輯
Private Sub Command5_Click()
Dim i As Integer
If MsgBox("真的要修改記錄嗎?", vbYesNo, "提示!") = vbNo Then
Exit Sub
End If
For i = 0 To 6
Text1(i).Locked = False
Next i
Text1(0).SetFocus '光標位置
End Sub
'儲存
Private Sub Command6_Click()
If MsgBox("真的要儲存嗎?", vbYesNo, "提示!") = vbNo Then
Exit Sub
Else
MsgBox ("儲存成功!")
End If
Call savedata
End Sub
'增加
Private Sub Command7_Click()
Dim i As Integer
Call savedata
For i = o To 6
Text1(i).Locked = False
Text1(i).Text = ""
Next i
Text1(0).SetFocus '光标
last = last + 1
current = last
Call savedata
Call showdata
End Sub
'删除
Private Sub Command8_Click()
Dim i As Integer, j As Integer
For i = 0 To 6
Text1(i).Locked = True
Next i
Call savedata
If MsgBox("确定要删除?", vbYesNo, "提示!") = vbNo Then
Exit Sub
End If
Open "exam.tmp" For Random As #3 Len = Len(student)
j = 1
For i = 1 To last
If i <> current Then
Get #1, i, student
Put #3, j, student
j = j + 1
End If
Next i
Close #1
Kill myfile
Close #3
Name "exam.tmp" As myfile
Open myfile For Random As #1 Len = Len(student)
last = last - 1
If current > last Then current = last
Call showdata
End Sub
'浏览
Private Sub Command9_Click()
Form2.Hide
Form3.Show
End Sub
'返回
Private Sub Command10_Click()
Form2.Hide
Form1.Show
End Sub
'返回后去主窗口
Private Sub Form_Unload(Cancel As Integer)
Form1.Show
End Sub