none
vb2005保存图片后如何让图片通过查询显示到picturebox.image里面去 RRS feed

  • 问题

  • 保存图片的代码:

     

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click

     

    Dim data(fs.Length) As Byte
                fs.Read(data, 0, Int(fs.Length))
                Dim prm3 As New SqlClient.SqlParameter("@photo", SqlDbType.VarBinary, Int(fs.Length), ParameterDirection.Input, False, 0, 0, "", DataRowVersion.Current, data)

     

    dim sql as string

    sql='insert into ygdab(ygtx) values (@photo)'

    exec3(sql,prm3)

     

    msgbox("添加成功")

     

    end sub

     

    显示图片的代码:

    Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
            Try
                Dim sql As String
                Dim mrc As DataTable
                sql = "select * from ygdab where ygid ='" & e.Node.Text.Split(" ")(0) & "'"
                mrc = exec(sql)
                If mrc.Rows.Count > 0 Then
                    Me.PictureBox1.Image = mrc.Rows(0).Item("ygtx")     '''无法强制转换
                    Exit Sub
                Else
                    MsgBox("没有数据", MsgBoxStyle.Information, "提示")
                    Exit Sub
                End If

            Catch ex As Exception
                MsgBox(ex.ToString, MsgBoxStyle.Information, "try")
            End Try
        End Sub

     

     

     

    请版主帮忙给我看看该怎么修改????

    谢谢

    2008年8月26日 8:27

答案

  •  

    换成 CType 即可

     

     

    Code Snippet

    Dim cnString As String = "Data Source=(local)\SQL2005;Initial Catalog=Example;Integrated Security=SSPI;"
    Dim cn As New System.Data.SqlClient.SqlConnection(cnString)

    Dim cmd As New System.Data.SqlClient.SqlCommand("select img from Photos where StudentID = 1", cn)

    Dim da As New System.Data.SqlClient.SqlDataAdapter(cmd)
    Dim ds As New DataSet()
    da.Fill(ds, "Photos")
    Dim mrc As DataTable
    mrc = ds.Tables("Photos")
    Dim c As Integer = mrc.Rows.Count
    If c > 0 Then
        Dim byteBLOBData As Byte()
        byteBLOBData = CType(mrc.Rows(0)("img"), Byte())
        Dim stmBLOBData As New System.IO.MemoryStream(byteBLOBData)
        Me.PictureBox1.Image = Image.FromStream(stmBLOBData)
    End If
    cn.Close()

     

     

    2008年8月26日 12:47
    版主

全部回复

  • Dim byteData() As Byte
    byteData = CByte(mrc.Rows(0).Item("ygtx"))
    Dim stmData as MemoryStream  = new MemoryStream(byteData)
    PictureBox1.Image = Image.FromStream(stmData)
    2008年8月26日 9:00
    版主
  •  

    2楼的朋友你给的代码我放上去出错

     

    代码如下:

     

    Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
            Try
                Dim sql As String
                Dim mrc As DataTable
                sql = "select * from ygdab where ygid ='" & e.Node.Text.Split(" ")(0) & "'"
                mrc = exec(sql)
                    Dim byteData() As Byte
                    byteData = CByte(mrc.Rows(0).Item("ygtx"))
                    Dim stmData As MemoryStream = New MemoryStream(byteData)
                    PictureBox1.Image = Image.FromStream(stmData)
                    Exit Sub
                   End Sub

    出错原因:

       类型"byte" 的值无法转换为" byte的一维数组 "

     

     

    2008年8月26日 9:35
  •  

    换成 CType 即可

     

     

    Code Snippet

    Dim cnString As String = "Data Source=(local)\SQL2005;Initial Catalog=Example;Integrated Security=SSPI;"
    Dim cn As New System.Data.SqlClient.SqlConnection(cnString)

    Dim cmd As New System.Data.SqlClient.SqlCommand("select img from Photos where StudentID = 1", cn)

    Dim da As New System.Data.SqlClient.SqlDataAdapter(cmd)
    Dim ds As New DataSet()
    da.Fill(ds, "Photos")
    Dim mrc As DataTable
    mrc = ds.Tables("Photos")
    Dim c As Integer = mrc.Rows.Count
    If c > 0 Then
        Dim byteBLOBData As Byte()
        byteBLOBData = CType(mrc.Rows(0)("img"), Byte())
        Dim stmBLOBData As New System.IO.MemoryStream(byteBLOBData)
        Me.PictureBox1.Image = Image.FromStream(stmBLOBData)
    End If
    cn.Close()

     

     

    2008年8月26日 12:47
    版主
  • 谢谢你了 ...OK

    非常感谢孟宪会同学.......

     

    2008年8月26日 23:14