我正在尝试检索保存到.mdf数据库中的图像。我需要使用代码来提取数据并将其显示在pictureBox
。
如何从.mdf数据库中检索图像并将其显示在带有pictureBox
的VB应用程序中?
答案 0 :(得分:0)
试试这个
Dim path As String = (Microsoft.VisualBasic.Left(Application.StartupPath, Len(Application.StartupPath) - 9))
Dim con As New SqlConnection("Data Source=[Server Name];AttachDbFilename=" & _
path & "Database1.mdf;Integrated Security=True;User Instance=True")
Dim cmd As SqlCommand
cmd = New SqlCommand("select photo from Information where name='image1'", con)
Dim imageData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())
If Not imageData Is Nothing Then
Using ms As New MemoryStream(imageData, 0, imageData.Length)
ms.Write(imageData, 0, imageData.Length)
PictureBox1.BackgroundImage = Image.FromStream(ms, True)
End Using
End If