Hi jmcilhinney,
Thanks for this code you posted to help novices like me get around some problems.
As I said, I'm a novice to programming. I'm presently developping a database (db) programme for my cooperative society. I'm using Visual Basic 2008 express edition connected to a MS Access database.
One of the forms (MemberProfile) is meant to display the profile of each member with a picture of the member. I'm actually having problem with the picture. I got this code to load the picture into the picturebox:
Code:
Private Sub BrowseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BrowseButton.Click
Dim MyRow As Stream = Nothing
Dim fo As New OpenFileDialog
If fo.ShowDialog = DialogResult.OK Then
Try
MyRow = fo.OpenFile()
If (MyRow IsNot Nothing) Then
M_photoPictureBox.Load(fo.FileName)
fo.OpenFile()
End If
Catch ex As Exception
MessageBox.Show("Cannot read file from disk. Original error: " & ex.Message)
Finally
' Check this again, since we need to make sure we didn't throw an exception on open.
If (MyRow IsNot Nothing) Then
MyRow.Close()
End If
End Try
End If
End Sub
After loading the picture, I need to save it into the db and then be able to load each member's picture in the picturebox whenever the member's profile is displayed. With your sample code I tried to work something out but with no success. This is the code I have made from your sample code:
- this is to save the picture
Code:
Private Sub UploadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles UploadButton.Click
Dim connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Users\client\Documents\Visual Studio 2008\Projects\SavingsLoansCalc.mdb")
Dim command As New OleDbCommand("UPDATE Member SET m_photo = @m_photo WHERE m_accnumber = ?", connection)
'Create an Image object.
Using picture As Image = Image.FromFile("C:\Users\client\Pictures\pass2.jpg")
'Create an empty stream in memory.
Using stream As New IO.MemoryStream
'Fill the stream with the binary data from the Image.
picture.Save(stream, Imaging.ImageFormat.Jpeg)
'Get an array of Bytes from the stream and assign to the parameter.
command.Parameters.Add("@m_photo", OleDbType.VarBinary).Value = stream.GetBuffer()
End Using
End Using
connection.Open()
command.ExecuteNonQuery()
connection.Close()
End Sub
- and this is to load it back for preview
Code:
Private Sub MemberProfileForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'SavingsLoansDataSet.Liste_of_Banks' table. You can move, or remove it, as needed.
Me.Liste_of_BanksTableAdapter.Fill(Me.SavingsLoansDataSet.Liste_of_Banks)
'TODO: This line of code loads data into the 'SavingsLoansDataSet.Liste_of_Banks' table. You can move, or remove it, as needed.
Me.MemberTableAdapter.Fill(Me.SavingsLoansDataSet.Member)
Dim connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Users\client\Documents\Visual Studio 2008\Projects\SavingsLoansCalc.mdb")
Dim command As New OleDbCommand("SELECT m_photo FROM Member WHERE m_accnumber = ?", connection)
connection.Open()
Dim pictureData As Byte() = DirectCast(command.ExecuteScalar(), Byte())
connection.Close()
Dim picture As Image = Nothing
'Create a stream in memory containing the bytes that comprise the image.
Using stream As New IO.MemoryStream(pictureData)
'Read the stream and create an Image object from the data.
picture = Image.FromStream(stream)
End Using
End Sub
Please, what am I missing out? I need to be able to load the picture from a specific or any drive, have each member's picture saved and displayed during the preview of the profile.
Thanks in advance. Waiting impatiently to hear from you.