WHAT'S NEW?
Loading...

Adding images to database in VB.Net





Adding Images that store into database in Visual Basic.Net


Save Image
Load Image









This is the line of code that save images to database.
Imports System.Drawing.Image
Imports System.IO
Public Class Form2
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load        btnimage.Enabled = False
        FirstnameTextBox.Enabled = False
        LastnameTextBox.Enabled = False
        MiddlenameTextBox.Enabled = False
        GenderTextBox.Enabled = False
        'TODO: This line of code loads data into the 'DbSaveImgDataSet.tblsaveimg' table. You can move, or remove it, as needed.
        Me.TblsaveimgTableAdapter.Fill(Me.DbSaveImgDataSet.tblsaveimg)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click        If FirstnameTextBox.Text = "" Or LastnameTextBox.Text = "" Then
            MessageBox.Show("Please fill the textbox")        Else
            Me.Validate()           Me.TblsaveimgBindingSource.EndEdit()            Me.TblsaveimgTableAdapter.Insert(FirstnameTextBox.Text, LastnameTextBox.Text, MiddlenameTextBox.Text, GenderTextBox.Text, convertimage(Me.PictureBox1.Image))
Me.TblsaveimgTableAdapter.Fill(DbSaveImgDataSet.tblsaveimg)            Me.TblsaveimgTableAdapter.Update(DbSaveImgDataSet.tblsaveimg)
            MessageBox.Show("Completed")        End If
        For Each item As Control In Controls            If TypeOf item Is TextBox Then
                item.Text = String.Empty
            End If
        Next
    End Sub

    Public Function convertimage(ByVal myimage As Image) As Byte()        Dim mystream As New MemoryStream()        myimage.Save(mystream, System.Drawing.Imaging.ImageFormat.Jpeg)
        Dim mybytes(mystream.Length - 1) As Byte
        mystream.Position = 0        mystream.Read(mybytes, 0, mystream.Length)
        Return mybytes
    End Function

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnimage.Click
        Me.OpenFileDialog1.FileName = Nothing
        Me.OpenFileDialog1.ShowDialog()
        If Not Me.OpenFileDialog1.FileName = Nothing Then
            Me.PictureBox1.ImageLocation = Me.OpenFileDialog1.FileName
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click        TblsaveimgBindingSource.AddNew()        FirstnameTextBox.Enabled = True
        LastnameTextBox.Enabled = True
        MiddlenameTextBox.Enabled = True
        GenderTextBox.Enabled = True
        btnimage.Enabled = True
        FirstnameTextBox.Focus()    End Sub
End Class

Note: You are going to provide or create your own database to save your data.


0 comments:

Post a Comment