查了很多资料,都是先将数据库中的信息先写成一张图片文件,然后显示这个图片文件,有没有办法不写文件而直接显示?
解决方案 »
- 【请求援助】请大家联名抗议csdn的blog系统的速度问题
- VB6压缩解压缩字符串:找到了一个压缩解压字符串的类模块,但有些问题,请大家看看?
- 我想读取一个程序的内存,但是怎么告诉VB,是哪个程序,具体代码怎么写?谢谢
- VB中水晶报表的问题
- 怎样将datagrid控件中的内容直接打印出来啊?
- 求一软件!
- 如何设置文本框,使其内容不能被更改?
- 帮我看看到底是什么问题?还有一个小问题要问一下!
- vb6访问数据库
- 今天凌晨(北京时间)进行的世界青年足球锦标赛小组赛,中国对美国的小组首场比赛中,中国队1:0小胜美国,出线前景一偏光明。
- sql server 海量数据库中修改记录时产生“超时已过期”的错误!
- ADODC控件对数据库操作的方法
bmType As Long
bmWidth As Long
bmHeight As Long
bmWidthBytes As Long
bmPlanes As Integer
bmBitsPixel As Integer
bmBits As Long
End Type
'--------------------------------------------------------
' Procedure : SaveImage
' Purpose : Saves a StdPicture object in a byte array.
'--------------------------------------------------------
'
Public Function SaveImage( _
ByVal image As StdPicture) As Byte()
Dim abData() As Byte
Dim oPersist As IPersistStream
Dim oStream As IStream
Dim lSize As Long
Dim tStat As STATSTG ' Get the image IPersistStream interface
Set oPersist = image
' Create a stream on global memory
Set oStream = CreateStreamOnHGlobal(0, True)
' Save the picture in the stream
oPersist.Save oStream, True
' Get the stream info
oStream.Stat tStat, STATFLAG_NONAME
' Get the stream size
lSize = tStat.cbSize * 10000
' Initialize the array
ReDim abData(0 To lSize - 1)
' Move the stream position to
' the start of the stream
oStream.Seek 0, STREAM_SEEK_SET
' Read all the stream in the array
oStream.Read abData(0), lSize
' Return the array
SaveImage = abData
' Release the stream object
Set oStream = Nothing End Function '--------------------------------------------------------
' Procedure : LoadImage
' Purpose : Creates a StdPicture object from a byte array.
'--------------------------------------------------------
'
Public Function LoadImage( _
ImageBytes() As Byte) As StdPicture
Dim oPersist As IPersistStream
Dim oStream As IStream
Dim lSize As Long
' Calculate the array size
lSize = UBound(ImageBytes) - LBound(ImageBytes) + 1
' Create a stream object
' in global memory
Set oStream = CreateStreamOnHGlobal(0, True)
' Write the header to the stream
oStream.Write &H746C&, 4&
' Write the array size
oStream.Write lSize, 4&
' Write the image data
oStream.Write ImageBytes(LBound(ImageBytes)), lSize
' Move the stream position to
' the start of the stream
oStream.Seek 0, STREAM_SEEK_SET
' Create a new empty picture object
Set LoadImage = New StdPicture
' Get the IPersistStream interface
' of the picture object
Set oPersist = LoadImage
' Load the picture from the stream
oPersist.Load oStream
' Release the streamobject
Set oStream = Nothing
End Function
2.从rs中取得除图片以外的其它相关数据
3.以ADODB.Stream方式读取rs中的图片数据并利用ADODB.Stream的save方法将它存储为一个图片文件
4.后继的其它处理,其中一个是利用loadpicture方法及picture属性将存储的图片文件显于界面上的控件之中.
http://topic.csdn.net/u/20070521/12/aebdd5f2-eb70-4207-925b-fae1ccfda3ae.html
可以用
ADODB.Stream 的LoadFromFile 代替
写入数据库
也可以直接
.Fields("photo") = iStm.Read iStm是 一个ADODB.Stream对象
读数据库里的图片数据(其实就是二进制数组)到picture框
可以用 Fields("photo") 或者 Fields("photo").value 来代替(表示)数组
如果用了 ADODB.Stream 可以用 iStm.Read 来代替数组
'2.5版本以下不支持Stream对象
Option Explicit
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;Data Source=" & App.Path & "\img.mdb"
'下面的语句是连接sqlserver数据库的.
'iConcstr = "Provider=SQLOLEDB.1;Persist Security Info=True;" & _
' "User ID=sa;Password=;Initial Catalog=test;Data Source=yang"
Set iConc = New ADODB.Connection
iConc.Open iConcstr
Command1.Caption = "read"
Command2.Caption = "save"
End Sub
Private Sub Form_Unload(Cancel As Integer)
iConc.Close
Set iConc = Nothing
End Sub
网上的一个代码 准备改这个代码
'** 引用 Microsoft ActiveX Data Objects 2.5 Library 及以上版本
'2.5版本以下不支持Stream对象
Option Explicit
Dim iConcstr As String
Dim iConc As ADODB.Connection
'保存文件到数据库中
Private 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
Private 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 Image1.Picture = LoadImage(iRe("photo").GetChunk(iRe("photo").ActualSize))
iRe.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;Data Source=" & App.Path & "\img.mdb"
'下面的语句是连接sqlserver数据库的.
'iConcstr = "Provider=SQLOLEDB.1;Persist Security Info=True;" & _
' "User ID=sa;Password=;Initial Catalog=test;Data Source=yang"
Set iConc = New ADODB.Connection
iConc.Open iConcstr
Command1.Caption = "read"
Command2.Caption = "save"
End Sub
Private Sub Form_Unload(Cancel As Integer)
iConc.Close
Set iConc = Nothing
End Sub
'--------------------------------------------------------
' Procedure : SaveImage
' Purpose : Saves a StdPicture object in a byte array.
'--------------------------------------------------------
'
Private Function SaveImage( _
ByVal image As StdPicture) As Byte()
Dim abData() As Byte
Dim oPersist As IPersistStream
Dim oStream As IStream
Dim lSize As Long
Dim tStat As STATSTG ' Get the image IPersistStream interface
Set oPersist = image
' Create a stream on global memory
Set oStream = CreateStreamOnHGlobal(0, True)
' Save the picture in the stream
oPersist.Save oStream, True
' Get the stream info
oStream.Stat tStat, STATFLAG_NONAME
' Get the stream size
lSize = tStat.cbSize * 10000
' Initialize the array
ReDim abData(0 To lSize - 1)
' Move the stream position to
' the start of the stream
oStream.Seek 0, STREAM_SEEK_SET
' Read all the stream in the array
oStream.Read abData(0), lSize
' Return the array
SaveImage = abData
' Release the stream object
Set oStream = NothingEnd Function'--------------------------------------------------------
' Procedure : LoadImage
' Purpose : Creates a StdPicture object from a byte array.
'--------------------------------------------------------
'
Private Function LoadImage( _
ImageBytes() As Byte) As StdPicture
Dim oPersist As IPersistStream
Dim oStream As IStream
Dim lSize As Long
' Calculate the array size
lSize = UBound(ImageBytes) - LBound(ImageBytes) + 1
' Create a stream object
' in global memory
Set oStream = CreateStreamOnHGlobal(0, True)
' Write the header to the stream
oStream.Write &H746C&, 4&
' Write the array size
oStream.Write lSize, 4&
' Write the image data
oStream.Write ImageBytes(LBound(ImageBytes)), lSize
' Move the stream position to
' the start of the stream
oStream.Seek 0, STREAM_SEEK_SET
' Create a new empty picture object
Set LoadImage = New StdPicture
' Get the IPersistStream interface
' of the picture object
Set oPersist = LoadImage
' Load the picture from the stream
oPersist.Load oStream
' Release the streamobject
Set oStream = Nothing
End Function这样就不用保存到文件了 这个代码引用了 olelib.tlb
感觉上 还可以继续缩短
ISTREAM 和adodb.STREAM 应该是差不多的东西