图片保存到资料库以后再读出来变大了很多,而且无法另存为文件?
我将图片读入资料库了,在电脑上可以显示了,(使用Image对象的pictureData属性。数据库里是Image字段)
可是我想将这个字段的内容读出并另为图片文件时,
,(使用ADODB.Stream对象的Write方法,再用savetoFile方法。)
却发现大小增大了几十倍,而且无法打开图片了。怎么办?
另存图片的代码如下:
Dim rss As New ADODB.Stream
Dim strFn As String, rsK As New ADODB.Recordset
strFn = "C:\Autodocs\ " & Me.DrvName & ".jpg "
strSQL = "select DrvPhoto from CN_Driver where DrvName= ' " & Me.DrvName & " ' "
rsK.Open strSQL, CurrentProject.Connection
rss.Mode = adModeReadWrite
rss.Type = adTypeBinary
rss.Open
rss.Write rsK(0)
rss.Read
rss.SaveToFile strFn, adSaveCreateOverWrite
请高手救命啊。。。
------解决方案--------------------使用流对象保存和显示图片
打开vb6,新建工程。
添加两个按钮,一个image控件
注意:Access中的photo字段类型为OLE对象.
SqlServer中的photo字段类型为Image
'** 引用 Microsoft ActiveX Data Objects 2.5 Library 及以上版本
‘2.5版本以下不支持Stream对象
Dim iConcstr As String
Dim iConc As ADODB.Connection
'保存文件到数据库中
Sub s_SaveFile()
Dim iStm As ADODB.Stream
Dim iRe As ADODB.Recordset
Dim iConcstr As String
'读取文件到内容
Set iStm = New ADODB.Stream
With iStm
.Type = adTypeBinary '二进制模式
.Open
.LoadFromFile App.Path + "\test.jpg "
End With
'打开保存文件的表
Set iRe = New ADODB.Recordset
With iRe
.Open "select * from img ", iConc, 1, 3
.AddNew '新增一条记录
.Fields( "photo ") = iStm.Read
.Update
End With
'完成后关闭对象
iRe.Close
iStm.Close
End Sub
Sub s_ReadFile()
Dim iStm As ADODB.Stream
Dim iRe As ADODB.Recordset
'打开表
Set iRe = New ADODB.Recordset
‘得到最新添加的纪录
iRe.Open "select top 1 * from img order by id desc ", iConc, adOpenKeyset, adLockReadOnly
'保存到文件
Set iStm = New ADODB.Stream
With iStm
.Mode = adModeReadWrite
.Type = adTypeBinary
.Open
.Write iRe( "photo ")
‘这里注意了,如果当前目录下存在test1.jpg,会报一个文件写入失败的错误.
.SaveToFile App.Path & "\test1.jpg "
End With
Image1.Picture = LoadPicture(App.Path & "\test1.jpg ")
'关闭对象
iRe.Close
iStm.Close
End Sub
Private Sub Command1_Click()
Call s_ReadFile
End Sub
Private Sub Command2_Click()
Call s_SaveFile
End Sub
Private Sub Form_Load()
'数据库连接字符串
iConcstr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False " & _