怎样从数据库得SQL Server Image字段,得到图片,得图片时不用先写到本地文件,要直接从字段转到Picture对象中

laoyang 2003-09-02 09:10:13
怎样从数据库得SQL Server Image字段,得到图片,得图片时不用先写到本地文件,要直接从字段转到Picture对象中
...全文
39 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ludgee 2003-09-03
  • 打赏
  • 举报
回复
上面是我的方法。使用了ADO的stream对象,但是用了文件存储。
ludgee 2003-09-03
  • 打赏
  • 举报
回复
Option Explicit
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset

Private Sub Command1_Click()

Dim stm As New ADODB.Stream

On Error GoTo HandleErr

stm.Type = adTypeBinary
stm.Open
stm.Write rst.Fields("logo").Value
stm.SaveToFile "c:\test.gif", adSaveCreateOverWrite
Image1.Picture = LoadPicture("c:\test.gif")

Kill "c:\test.gif"

stm.Close
Set stm = Nothing

ExitHere:
Exit Sub

HandleErr:
MsgBox "Error: " & Err.Number & ": " & Err.Description, vbOKOnly, "cmdBolb"
Resume ExitHere
Resume

End Sub

Private Sub Command2_Click()

Dim FileName As String
Dim stm As New ADODB.Stream

On Error GoTo HandleErr

'Image1.Picture = LoadPicture()

With CommonDialog1
.Filter = "BMP图形(*.bmp)|*.bmp|JPG图形(*.jpg)|*.jpg" & _
"|GIF图形(*.gif)|*.gif|CUR光标(*.cur)|*.cur" & _
"|ICO图标(*.ico)|*.ico|WMF图形(*.wmf)|*.wmf"
.ShowOpen
FileName = .FileName
End With

If FileName <> "" Then
Image1.Picture = LoadPicture(FileName)

stm.Type = adTypeBinary
stm.Open
stm.LoadFromFile FileName
rst.Fields("logo").Value = stm.Read
rst.Update
stm.Close

End If

Set stm = Nothing

ExitHere:
Exit Sub

HandleErr:
MsgBox "Error: " & Err.Number & ": " & Err.Description, vbOKOnly, "cmdBolb"
Resume ExitHere
Resume

End Sub

Private Sub Command3_Click()

Image1.Picture = LoadPicture()

End Sub

Private Sub Form_Load()

cnn.Open "Provider=SQLOLEDB.1;" & _
"Persist Security Info=False;" & _
"User ID=sa;Initial Catalog=pubs;" & _
"Data Source=yyjys2"
rst.Open "pub_info", cnn, adOpenKeyset, adLockOptimistic

End Sub

Private Sub Form_Unload(Cancel As Integer)

rst.Close
Set rst = Nothing
Set cnn = Nothing

End Sub
Ge 2003-09-03
  • 打赏
  • 举报
回复
Dim Pic As ADODB.Stream
Dim MsgText As String
Dim mrc As ADODB.Recordset
TxtSQL = "select * from Picture"
Set mrc = ExecuteSQL(TxtSQL, MsgText)
Set Pic = New ADODB.Stream
Pic.Type = adTypeBinary
Pic.Open
Pic.LoadFromFile "C:\Documents and Settings\Administrator\My Documents\My Pictures\1.jpg"
mrc.Fields("ID") = "2"
mrc.Fields(1) = Pic.Read
mrc.Update


Dim TxtSQL As String
Dim MsgText As String
Dim mrc As ADODB.Recordset

TxtSQL = "select * from Picture"
Set mrc = ExecuteSQL(TxtSQL, MsgText)
Set Pic = New ADODB.Stream
Pic.Type = adTypeBinary
Pic.Open
Pic.Write (mrc.Fields("Picture").Value)
Pic.SaveToFile "C:\2.jpg", adSaveCreateOverWrite
Picture1.Picture = LoadPicture("C:\2.jpg")
yijiansong 2003-09-02
  • 打赏
  • 举报
回复
用临时文件吧,用完就删除了,或者放到系统临时文件夹下
射天狼 2003-09-02
  • 打赏
  • 举报
回复
用临时文件吧,也没有什么难的,用完就删除了,或者放到系统临时文件夹下,难道会产生垃圾吗,WINDOWS本身就会产生一堆的垃圾~~
TechnoFantasy 2003-09-02
  • 打赏
  • 举报
回复
上面只是一个思路,我没有现成的代码。不过考虑到实现的复杂性,你最好是通过文件的方法来访问图片。
TechnoFantasy 2003-09-02
  • 打赏
  • 举报
回复
通过ADO的Stream对象可以将图片作为一个Stream返回:
http://support.microsoft.com/default.aspx?kbid=258038
然后通过Stream对象的Write 方法可以将内容写入到一个Byte数组,然后通过OleLoadPicture 函数写入到一个Picture中:

Option Explicit

Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type

Private Declare Function CreateStreamOnHGlobal Lib "ole32.dll" (ByRef hGlobal As Any, ByVal fDeleteOnResume As Long, ByRef ppstr As Any) As Long
Private Declare Function OleLoadPicture Lib "olepro32.dll" (ByVal lpStream As IUnknown, ByVal lSize As Long, ByVal fRunMode As Long, ByRef riid As GUID, ByRef lplpObj As Any) As Long
Private Declare Function CLSIDFromString Lib "ole32.dll" (ByVal lpsz As Long, ByRef pclsid As GUID) As Long

Private Const SIPICTURE As String = "{7BF80980-BF32-101A-8BBB-00AA00300CAB}"

' convert a byte array containing a resource to IPicture
Public Function PictureFromRes(ByRef b() As Byte) As IPicture
On Error Goto errorhandler

Dim istrm As IUnknown
Dim tGuid As GUID

If Not CreateStreamOnHGlobal(b(LBound(b)), False, istrm) Then
CLSIDFromString StrPtr(SIPICTURE), tGuid
OleLoadPicture istrm, UBound(b) - LBound(b) + 1, False, tGuid, PictureFromRes
End If

Set istrm = Nothing
Exit Function
errorhandler:
Debug.Print "Could not convert to IPicture!"
End Function
lihonggen0 2003-09-02
  • 打赏
  • 举报
回复
微软的答案:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;258038
lyhlhr 2003-09-02
  • 打赏
  • 举报
回复
回复人: TechnoFantasy(www.applevb.com) ( ) 信誉:130 2003-09-02 09:36:00 得分:0

5颗星,好牛!
lxcc 2003-09-02
  • 打赏
  • 举报
回复
用loadpicture("picfilepath\filename")
haal 2003-09-02
  • 打赏
  • 举报
回复
用ADO直接将图片框绑定到该该图片字段

7,789

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧