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