我的图片框未提供图像的输出

时间:2014-02-17 03:00:49

标签: vb.net

帮助我的代码没有运行但它正在捕捉我的错误消息,我想显示来自我的数据库的图像,我将它传递到我的图片框

'这是我的代码

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsearch.Click
        Try
            disconnect()
            connect()
            cmd = New Odbc.OdbcCommand("Select * FROM tblemployee WHERE lname ='" & Trim(TextBox1.Text.TrimEnd()) & "'", con)
            dr = cmd.ExecuteReader

            If dr.Read() Then

                PictureBox1.Image = (dr("emp_pix"))
            Else
                MessageBox.Show("No Information Record, Please Name!")
            End If
        Catch ex As Exception
            Debug.WriteLine("Please try again" & ex.Message)

        End Try
End Sub

1 个答案:

答案 0 :(得分:1)

您无法直接将字节显示为图像。你需要转换并显示它。

试试这个

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsearch.Click
        Try
            disconnect()
            connect()
            cmd = New Odbc.OdbcCommand("Select * FROM tblemployee WHERE lname ='" & Trim(TextBox1.Text.TrimEnd()) & "'", con)
            dr = cmd.ExecuteReader

            If dr.Read() Then
                Dim bytBLOBData() As Byte = dr("emp_pix")
                Dim stmBLOBData As New MemoryStream(bytBLOBData)
                PictureBox1.Image = Image.FromStream(stmBLOBData)
            Else
                MessageBox.Show("No Information Record, Please Name!")
            End If
        Catch ex As Exception
            Debug.WriteLine("Please try again" & ex.Message)

        End Try
End Sub