Code for browse-save-retrieve image
Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
Dim a As New OpenFileDialog
a.Filter = "Image(*.jpg,*.png,*.bmp,*.JPeg,*.Gif)|*.jpg;*.png;*.bmp;*.JPeg;*.Gif;"
a.Title = "Browse icon for ..."
If a.ShowDialog = DialogResult.OK Then
Dim img As Bitmap = Image.FromFile(a.FileName)
img.SetResolution(72, 72)
PictureBox1.BackgroundImage = img
PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
IsChanged = True
End If
End Sub
If IsChanged = True Then
IsChanged = False
Dim stream1 As New IO.MemoryStream
Dim img As Bitmap
img = PictureBox1.BackgroundImage
Using img
Using stream As New IO.MemoryStream
Try
img.Save(stream, Imaging.ImageFormat.Bmp)
Catch ex As Exception
Try
img.Save(stream, Imaging.ImageFormat.Png)
Catch ex1 As Exception
Try
img.Save(stream, Imaging.ImageFormat.Gif)
Catch ex2 As Exception
Try
img.Save(stream, Imaging.ImageFormat.MemoryBmp)
Catch ex3 As Exception
End Try
End Try
End Try
End Try
stream1 = stream
stream.Dispose()
End Using
End Using
Try
Cmd.CommandText = "update Dtl set Img=@Img"
Cmd.Parameters.Clear()
Cmd.Parameters.AddWithValue("@Img", stream1.GetBuffer)
Cmd.ExecuteNonQuery()
Cmd.Parameters.Clear()
stream1.Dispose()
Catch ex As Exception
End Try
Try
Dim dt As New DataTable
dt = DBGetData("select * from Dtl")
For i = 0 To dt.Rows.Count - 1
txtNm.Text = dt.Rows(i)("Nm")
txtAdd.Text = dt.Rows(i)("Add")
txtWebsite.Text = dt.Rows(i)("Website")
txtMailId.Text = dt.Rows(i)("MailId")
txtCont1.Text = dt.Rows(i)("ContNo1")
txtCont2.Text = dt.Rows(i)("ContNo2")
Dim ImgData As Byte() = DirectCast(dt.Rows(i)("Img"), Byte())
Dim stream As New IO.MemoryStream(ImgData)
Using stream
Img = Image.FromStream(stream)
End Using
stream.Dispose()
PictureBox1.BackgroundImage = Img
PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
Next
Catch ex As Exception
End Try
Happy Coding!
:)