Click here to Skip to main content
15,906,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I had been searching on internet for guide source of tutorials how to make this but i coudn't find any use one. So i will try to post here maybe someone will help me out.

How to make this 2 features
.accdb file with table:
- Users -> ID, Picture (OleDB)

When form loads to load picture from that table and display it on picturebox1
Save button to import picture from computer to the table and column.

Sorry to post this but really couldn't find good and newer tutorial with accsess db.

If someone could help me, purpose is learning and making projects for myself to learn and uderstand more and more about ms accsess and vb.net.
Thank you for support even if its to some thread where it can help me to create it.

What I have tried:

tryed old tutorials that were maded in 2010-2011 but not good options. Need something new and better maded for learning.
Posted
Updated 6-Jun-20 23:27pm

Create the access database and add the table that you want to use for storing the data. Image files use the BLOB datatype. However, sometimes it can be better to save images as files and just store their pathname in the database.

Create a form that includes a picturebox control. Add the code in the form load method to load the image into the picture box.

If you need help using specific controls then you can check their methods and properties on the MSDN documentation pages. For example PictureBox Class (System.Windows.Forms) | Microsoft Docs[^].
 
Share this answer
 
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim dialog As New OpenFileDialog()
        dialog.Title = "Browse Picture"
        dialog.Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG"
        If dialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            PictureBox1.Image = Image.FromFile(dialog.FileName)
            TextBox1.Text = dialog.FileName.ToString
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Try
            With cmd
                Dim ms As New IO.MemoryStream()
                PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
                Dim arrimage() As Byte = ms.GetBuffer
                conn.Open()
                .Connection = conn
                .CommandText = "UPDATE Table SET cPicture = @picture WHERE ID = 1"
                .Parameters.Add("@Picture", OleDbType.Binary).Value = arrimage
                .ExecuteNonQuery()
                ms.Close()
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            cmd.Dispose()
            If conn IsNot Nothing Then
                conn.Close()
            End If
        End Try

    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Try
            With cmd
                Dim stream As New IO.MemoryStream()
                conn.Open()
                .Connection = conn
                .CommandText = "select cPicture from Table where ID=1"
                Dim image As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())
                stream.Write(image, 0, image.Length)
                Dim bitmap As New Bitmap(stream)
                PictureBox1.Image = bitmap '--->I have used another picturebox to display image from database.
                stream.Close()
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            cmd.Dispose()
            If conn IsNot Nothing Then
                conn.Close()
            End If
        End Try

    End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900