Access数据库中的图片怎样与PictueBox绑定?

zwnylsf 2008-04-22 04:23:28
以OLE对象存入的图片,字段为picture,绑定到picturebox应该怎么写代码啊?
...全文
177 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
guoruixing 2008-04-23
  • 打赏
  • 举报
回复

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
DA = New SqlDataAdapter("select * from employees", cnn)
DA.Fill(DS)

'从数据库加载图片
'内存流
Dim BT() As Byte '字节数组
BT = DS.Tables(0).Rows(0).Item("Photo") '从数据库中读出的OLE对象
Dim MS As New MemoryStream(BT, 78, BT.Length - 78)
PictureBox1.Image = Image.FromStream(MS)


'Dim myfilestream As New System.IO.FileStream(Application.StartupPath & "\monkey.bmp", IO.FileMode.Create)
'myfilestream.Write(BT, 80, BT.Length - 1)
'myfilestream.Close()
'PictureBox1.Image = Image.FromFile(Application.StartupPath & "\monkey.bmp")
End Sub
guoruixing 2008-04-23
  • 打赏
  • 举报
回复
employee表中的photo中的信息是二进制的,其中有效的图片信息是从第78位开始的。所以当你想要转换图片信息的时候应该从第78位开始
水如烟 2008-04-23
  • 打赏
  • 举报
回复
水如烟 2008-04-23
  • 打赏
  • 举报
回复
Public Class Form1
Private db As New Global.LzmTW.Data.SqlSqlClientDatabase(".", "Northwind")
Private WithEvents binding As New BindingSource
Private Const position As Integer = 4 * 16 + 14

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.DataGridView1.DataSource = binding
binding.DataSource = db.GetDataTable("Employees")
End Sub

Private Sub binding_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles binding.PositionChanged
Dim bytes As Byte() = CType(CType(binding.Current, DataRowView).Row.Item("Photo"), Byte())

Dim stream As New IO.MemoryStream(bytes, position, bytes.Length - position)
Dim bitmap As New System.Drawing.Bitmap(stream)

Me.PictureBox1.Image = bitmap
End Sub
End Class
zwnylsf 2008-04-23
  • 打赏
  • 举报
回复
不是很了解,学习学习。除了这个之外,有没有其他的办法呢?
水如烟 2008-04-23
  • 打赏
  • 举报
回复
不对头
水如烟 2008-04-23
  • 打赏
  • 举报
回复
试了一下,Northwind的要从4*16+14处开始读数据才行
水如烟 2008-04-23
  • 打赏
  • 举报
回复
楼上的试试Northwind的Employees之Photo图像,那也不行.
guoruixing 2008-04-23
  • 打赏
  • 举报
回复
查看了一下,写成FS.Length-1能正常显示的是JPG格式图片,提示参数错误的是Gif图片
难道你的数据库中导入的图片是Gif格式的?
guoruixing 2008-04-23
  • 打赏
  • 举报
回复
晕,怎么不显示红色,我吐!
guoruixing 2008-04-23
  • 打赏
  • 举报
回复
呵呵,经测试,应该是你导入图片的时候产生的问题,导致读出的数据,不能构成PictureBox能够识别的图片格式,所以会提示出错。

导入图片的关键代码如下:

FS = New FileStream(mFileInfo.FullName, FileMode.Open, FileAccess.Read)
ReDim BT(FS.Length)
FS.Read(BT, 0, FS.Length - 1) '关键就是这里
FS.Close()
FS = Nothing
SqlConn.InsertPic(tbXmName.Text, cbType.Text, cbSubType.Text, BT)
BT = Nothing
'记录成功个数
intInTotal += 1


上面红色部分就是关键,从Byte数组读取数据时候,如果是写成FS.Length-1,则在显示图片界面,有部分图片可以成功显示,有部分图片就会提示“参数错误”,但如果代码修改为下面的写法,就不会出现问题


FS.Read(BT, 0, FS.Length) '把那个-1去掉,就可以了,多读取一个字节

guoruixing 2008-04-23
  • 打赏
  • 举报
回复
有可能你把图片写入数据库的时候出错了
水如烟 2008-04-22
  • 打赏
  • 举报
回复
我现在也不大清楚
zwnylsf 2008-04-22
  • 打赏
  • 举报
回复
能具体说说吗?
水如烟 2008-04-22
  • 打赏
  • 举报
回复
单维大小大于 65,535 像素时会引发这个错误.
水如烟 2008-04-22
  • 打赏
  • 举报
回复
应该是图像格式问题
zwnylsf 2008-04-22
  • 打赏
  • 举报
回复
BT = db.Tables("info").Rows(0)("图片")
MS = New IO.MemoryStream(BT)
Me.PctMyfriend.Image = Image.FromStream(MS)
还是出错,参数无效。
guoruixing 2008-04-22
  • 打赏
  • 举报
回复
你的代码,这句
information.Fill(db, "info")

那么,下面的,就是db.Tables("info"),

如果是information.Fill(db) ,才是db.Tables(0)

zwnylsf 2008-04-22
  • 打赏
  • 举报
回复
Private Sub FrmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim MS As IO.MemoryStream '内存流
Dim BT() As Byte '字节数组
Dim db As New DataSet
Dim path As String = Application.StartupPath.ToString & "\mydb\information\myfriend.mdb"
Dim myconnection As New OleDbConnection("provider=microsoft.jet.OLEDB.4.0;" & "data source=" & path & ";" & "Jet OLEDB:Database Password=98011054;")
Dim strsql As String = "select 姓名,性别,家庭电话,移动电话,生日,工作,性格,联系地址,图片 from info"
Dim mycommand As New OleDbCommand(strsql, myconnection)
myconnection.Open()
Dim information As OleDbDataAdapter = New OleDbDataAdapter(mycommand)
information.Fill(db, "info")
Me.TxtName.DataBindings.Add("text", db, "info.姓名")
Me.TxtHomenumber.DataBindings.Add("text", db, "info.家庭电话")
Me.TxtMobile.DataBindings.Add("text", db, "info.移动电话")
Me.TxtBirth.DataBindings.Add("text", db, "info.生日")
Me.TxtWork.DataBindings.Add("text", db, "info.工作")
Me.TxtAddress.DataBindings.Add("text", db, "info.联系地址")
Me.TxtChar.DataBindings.Add("text", db, "info.性格")
BT = db.Tables(0).Rows(0)("图片")
MS = New IO.MemoryStream(BT)
Me.PctMyfriend.Image = Image.FromStream(MS) With Me.LbxShow
.DataSource = db
.DisplayMember = "info.姓名"
End With
Me.ToolStripStatusLabel4.Text = Me.BindingContext(db, "info").Count
myconnection.Close()
Me.ToolStripStatusLabel5.Text = "浏览"
End Sub
可改了之后,出现错误“参数无效。”
guoruixing 2008-04-22
  • 打赏
  • 举报
回复
DT是DataTable
如果你定义的是DataSet,例如:
Dim DS as DataSet
'则
BT=DS.table(0).rows(0)("Pic1")
加载更多回复(2)

16,718

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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