在vb.net中显示来自SQL Server的图片框中的图像winforms

时间:2016-08-22 06:03:18

标签: sql-server vb.net winforms

在表image列中存储为0xFF ...如何在vb.net winforms的图片框中显示此格式文件?我尝试下面的代码,但没有工作,并显示readtimeout / writetimeout错误..帮助我...高兴谢谢..

Private Sub DisplayNameAttribute_UserImage()    
    Try
       strimage = "SELECT userimage from MKBLOGIN where empcode='" & str_empcode & "'"   
       imagedatabytes = objcommonvalidation.func_loadUserImage(strimage)    
       mem = New MemoryStream(imagedatabytes)    
       PictureBox1.Image = ToImage(imagedatabytes)       
    Catch ex As Exception

    End Try
 End Sub

 Public Function func_loadEmpImage(ByVal str_query As String) As Byte()
     Try
        Dim ds As New DataSet
        da = New SqlDataAdapter(str_query, con)
        da.Fill(ds)

        If ds.Tables(0).Rows.Count > 0 Then
            data = New Byte(0) {}
            data = ds.Tables(0).Rows(0)("userimage")
        End If

        Return data.ToArray()
    Catch ex As Exception
        Return data.ToArray()
    End Try
End Function

Public Shared Function ToImage(Data As Byte()) As Image
    If Data Is Nothing Then
        Return Nothing
    End If
    Dim img As Image
    Using stream As New MemoryStream(Data)
        Using temp As Image = Image.FromStream(stream)
            img = New Bitmap(temp)
        End Using
    End Using
    Return img
End Function

2 个答案:

答案 0 :(得分:0)

 cmd = New SqlCommand("Select userimage from table", con)
 dr = cmd.ExecuteReader
 dr.read

 Dim ImgStream As New IO.MemoryStream(CType(sqldr("userimage"), Byte()))
 PictureBox1.Image = Image.FromStream(ImgStream)
 ImgStream.Dispose()

答案 1 :(得分:0)

Dim stream As New IO.MemoryStream
    Dim img() As Byte
    img = table.Rows(0)(1)
    Dim ms As New MemoryStream(img)
    pddraw.Image = Image.FromStream(ms)
    conn.Open()
    command.ExecuteNonQuery()
    conn.Close()

这仅用于显示,不能用于编辑