Reputation: 41
I have an image in an access file, and I want to retrieve and display it in another Windows Forms form using the user id.
Code to save the image:
Dim img = PictureBox1. Image
Dim ms As New System.IO.MemoryStream
img.Save(ms, img.RawFormat)
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
bytImage = ms.ToArray()
ms.Close()
Code to retrieve the image:
con.ConnectionString = dbProvider & dbSource
con.Open()
Dim userIdPro = Transfer.userIdPro
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 fs = New MemoryStream
fs.Write(MyByte, 0, MyByte.Length)
fs.Flush()
Dim MyImg = Image.FromStream(fs, True)
MyImg.Save(dr.Item("ID").ToString & ".JPG", System.Drawing.Imaging.ImageFormat.Jpeg)
PictureBox1.Image = MyImg
fs.Close()
fs = Nothing
End While
con.Close()
End Sub
I can't bring the binary data into dr
; it is always empty.
Upvotes: 2
Views: 2600
Reputation: 775
I put your code calling the function bytesToImage(...)`. You don't need to save the image locally, unless you have a reason to do it.
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()
You class should have the Image
property as Byte()
<Table("User_info")>
Public Class User
Public Property Photo As Byte()
End Class
Using the following functions:
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
To save the image into the database:
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
To fetch the image from the database:
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
Upvotes: 1