Я поставил твой код, вызывая функцию bytesToImage (...) `.Вам не нужно сохранять изображение локально, если у вас нет причин для этого.
con.ConnectionString = dbProvider & dbSource
con.Open()
Dim userIdPro = Transfer.userIdPro
// Don't concatenate your parameters. This is a bad practice and
// exposes your application to SQL injection attacks. Use SQL
// parameters instead.
Dim query = ("SELECT Image FROM User_info WHERE ID = " & userIdPro & ";")
Dim ds As New DataSet
Dim dr As OleDb.OleDbDataReader
Dim cm = New OleDb.OleDbCommand(query, con)
dr = cm.ExecuteReader
While dr.Read()
Dim MyByte = dr.Item("Value")
Dim MyImg As Image
If MyByte IsNot Nothing Then
// You do not need to save it, just convert to an image
// type and set it to your PictureBox1 control.
MyImg = bytesToImage(MyByte)
PictureBox1.Image = MyImg
End If
End While
con.Close()
Ваш класс должен иметь свойство Image
как Byte()
<Table("User_info")>
Public Class User
Public Property Photo As Byte()
End Class
Использование следующих функций:
Public Function imageToBytes(ByVal imageIn As System.Drawing.Image) As Byte()
Dim ms As MemoryStream = New MemoryStream()
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Return ms.ToArray()
End Function
Public Function bytesToImage(ByVal byteArrayIn As Byte()) As Image
Dim ms As MemoryStream = New MemoryStream(byteArrayIn)
Dim returnImage As Image = Image.FromStream(ms)
Return returnImage
End Function
Чтобы сохранить изображение в базе данных:
Public Sub SaveImage()
Using context = New ProjectDb()
Dim user = New User() With {
.Id = Guid.NewGuid,
.Photo = imageToBytes(PictureBox1.Image)
}
context.Users.Add(user)
context.SaveChanges()
End Using
End Sub
Чтобы извлечь изображение из базы данных:
Public Function GetImage(ByVal id As Guid) As Image
Using context = New ProjectDb()
Dim image As Image
Dim user As User = context.Users.FirstOrDefault(Function(x) x.Id = id)
If user IsNot Nothing Then
image = bytesToImage(user.Photo)
PictureBox2.Image = image
End If
End Using
End Function