主题:图片问题!!!
dengxi
[专家分:310] 发布于 2005-11-18 13:22:00
怎么把图片框里面的图片存进ACCESS数据库。。然后怎么又把图片读出来放到图片框里面
又怎么样才能和图片框绑定。。以翻阅记录呢。
回复列表 (共12个回复)
沙发
宝宝啊呜 [专家分:990] 发布于 2005-11-19 10:40:00
SQL的我有代码,星期一上班了给你
板凳
dengxi [专家分:310] 发布于 2005-11-19 12:17:00
啊。谢了
3 楼
宝宝啊呜 [专家分:990] 发布于 2005-11-21 12:42:00
Public Function CheckBm()
abel2.Text = "第" & (bm.Position + 1) & "张,共" & bm.Count & "张"
End Function
Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click
Me.Close()
End Sub
Private Sub tpgl_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'图片的显示
Dim cn As New SqlConnection(connstr)
Dim cmd As New SqlCommand("SELECT * FROM picture where dh='" & Me.Text.Substring(3) & "'", cn)
da = New SqlDataAdapter(cmd)
' Dim ds As New DataSet
da.Fill(ds, "photo")
Dim c As Integer = ds.Tables("photo").Rows.Count
If c > 0 Then
Dim stmBLOBData As New MemoryStream(bytBLOBData)
PictureBox1.Image = Image.FromStream(stmBLOBData)
End If
bm = Me.BindingContext(ds, "photo")
CheckBm()
Public imagefilepath As String '= ""
Private Sub BtnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOpen.Click
'图片路径的打开
Me.OpenFileDialog1.InitialDirectory = Application.StartupPath
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
imagefilepath = OpenFileDialog1.FileName '此时,路径已经返回到imagefilepath中去了
PictureBox1.Image = Image.FromFile(imagefilepath)
End If
End Sub
Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
'图片的保存
Dim cn As New SqlConnection(connstr)
Dim insertcmd As String = "insert into picture values ( '" & Me.Text.Substring(3).Trim & "',@pic) "
Dim cmd As SqlCommand
cmd = New SqlCommand(insertcmd, cn)
Try
Dim fsBLOBFile As New FileStream(imagefilepath, FileMode.Open, FileAccess.Read)
Dim bytBLOBData(fsBLOBFile.Length() - 1) As Byte
fsBLOBFile.Read(bytBLOBData, 0, bytBLOBData.Length)
fsBLOBFile.Close()
Dim prm As New SqlParameter("@pic", SqlDbType.VarBinary, _
bytBLOBData.Length, ParameterDirection.Input, False, _
0, 0, Nothing, DataRowVersion.Current, bytBLOBData)
cmd.Parameters.Add(prm)
cn.Open()
cmd.ExecuteNonQuery()
cn.Close()
MessageBox.Show("图片保存成功!!")
ds.Clear()
da.Fill(ds, "photo")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
CheckBm()
End Sub
Private Sub Btndelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btndelete.Click
'图片的删除
Dim cn As New SqlConnection(connstr)
Dim delcmd As String = "delete from picture where dh ='" & Me.Text.Substring(3).Trim & "'"
Dim cmd As SqlCommand
cmd = New SqlCommand(delcmd, cn)
cn.Open()
cmd.ExecuteNonQuery()
cn.Close()
MessageBox.Show("图片已经删除!!")
PictureBox1.Image = Nothing '图片删除后,显示为空白
Me.BtnOpen.Enabled = True
Me.BtnSave.Enabled = True
Me.BtnClose.Enabled = True
ds.Clear()
da.Fill(ds, "photo")
CheckBm()
End Sub
4 楼
宝宝啊呜 [专家分:990] 发布于 2005-11-21 12:42:00
'前一张
Private Sub BtnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPrev.Click
If Me.BindingContext(ds, "photo").Position > 0 Then
Me.BindingContext(ds, "photo").Position = Me.BindingContext(ds, "photo").Position - 1
Else
Me.BindingContext(ds, "photo").Position = Me.BindingContext(ds, "photo").Count - 1
End If
Dim bytBLOBData() As Byte = ds.Tables("photo").Rows(Me.BindingContext(ds, "photo").Position)("pic")
Dim stmBLOBData As New MemoryStream(bytBLOBData)
PictureBox1.Image = Image.FromStream(stmBLOBData)
CheckBm()
End Sub
'后一张
Private Sub BtnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNext.Click
If Me.BindingContext(ds, "photo").Position >= Me.BindingContext(ds, "photo").Count - 1 Then
Me.BindingContext(ds, "photo").Position = 0
Else
Me.BindingContext(ds, "photo").Position = Me.BindingContext(ds, "photo").Position + 1
End If
Dim bytBLOBData() As Byte = ds.Tables("photo").Rows(Me.BindingContext(ds, "photo").Position)("pic")
Dim stmBLOBData As New MemoryStream(bytBLOBData)
PictureBox1.Image = Image.FromStream(stmBLOBData)
CheckBm()
End Sub
5 楼
ryowu [专家分:6470] 发布于 2005-11-21 12:52:00
此帖可作为示例帖:如何使用VB.NET从数据库读写图片
6 楼
dengxi [专家分:310] 发布于 2005-11-21 16:37:00
宝宝。。。给我点详细的解释啊。!!!!!!!!!!
我不懂哟。。。。怎么变成ACCESS的。。我搞了一下午结果弄不出来啊。。!!!
7 楼
宝宝啊呜 [专家分:990] 发布于 2005-11-21 18:13:00
Access的应该就是连接改一下吧?我这个能算是开源了不??呵呵
8 楼
dengxi [专家分:310] 发布于 2005-11-21 20:47:00
宝宝。。昏迷。。我不懂。。有些东西。。比如说添加的那一块。。你有些东西是没有声明的啊。我看不懂是什么意思!!!!
9 楼
dengxi [专家分:310] 发布于 2005-11-21 22:02:00
宝宝。。图片的添加做起了。。但是显示有问题了。。问题错在这句话上面
PictureBox1.Image = Image.FromStream(stmBLOBData)
说使用了什么无效参数。。我找不到错了。。
窗体加载的代码
Dim myconn As New OleDb.OleDbConnection(Conn)
Dim cmd As New OleDb.OleDbCommand("select * from stuinfo", myconn)
Dim Da As New OleDb.OleDbDataAdapter(cmd)
Da.Fill(ds, "user")
Dim C As Integer = ds.Tables("user").Rows.Count
Dim bytBLOBData As Byte
If C > 0 Then
Dim stmBLOBData As New System.IO.MemoryStream(bytBLOBData)
PictureBox1.Image = Image.FromStream(stmBLOBData)
End If
Dim bm = Me.BindingContext(ds, "user")
10 楼
宝宝啊呜 [专家分:990] 发布于 2005-11-23 12:09:00
漏掉的只是很简单的定义
Public ds As New DataSet
Dim da As SqlDataAdapter
Dim bm As BindingManagerBase
命名空间
Imports System.IO
Imports System.Drawing.Imaging
你那个少了一个指定图片列的参数。你对比下我贴的就知道了。
我来回复