' Load picture from computer to PICTUREBOX
Private Sub btnPicture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPicture.Click
Dim a As OpenFileDialog = New OpenFileDialog
a.Filter = ("Image Files|*.*|Bitmap|*.bmp|GIF|*.gif|Icon|*.ico|JPEG|*.jpg|PNG|*.png")
If a.ShowDialog() = Windows.Forms.DialogResult.OK Then
PictureBox1.Image = Image.FromFile(a.FileName)
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
End If
End Sub
' then
' InsertPicture from PictureBox to SQL Table, if no Picture insert NULL value
Private Sub btnSavePicture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSavePicture.Click
If Not PictureBox1.Image Is Nothing Then
Dim picStream As New MemoryStream
PictureBox1.Image.Save(picStream, Imaging.ImageFormat.Png)
PicByte = picStream.GetBuffer()
cmd.Parameters.Add(New SqlParameter("@Picture", SqlDbType.Image)).Value = PicByte
Else
cmd.Parameters.Add(New SqlParameter("@Picture", SqlDbType.Image)).Value = Convert.DBNull
End If
Try
Me.conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
MsgBox("Insert data to Database successful!")
Catch ex As SqlException
conn.Close()
MsgBox(ex.ToString)
End Sub
Nguồn: Sharecode.vn