To Download Source Code Click Here > image to database.rar
- Open Visual Studio and Click on New Project Windows from template
- Place PictureBox, ListBox, OpenFileDialog, and Four Buttons
- Set Name Properties of :
- PictureBox = pctImage,
- ListBox = lstImages,
- Button1 = btnBrowse,
- Button2 = btnSave,
- Button3 = btnDelete,
- Button4 = btnExit
- Set Text Properties of :
- Button1 = Browse,
- Button2 = Save,
- Button3 = Delete,
- Button4 = Exit.
- Right Click on the form and Click on View code
- And type the following Code.
Imports System.Data.SQLite
Imports System.IO
Imports System.Drawing.Image
Public Class Form1
Private Conn As New SQLiteConnection("data Source =" & Application.StartupPath & "\Images.img")
Private Adpt As SQLiteDataAdapter
Private strFileName As String
Private Sub DisplayInList()
Dim strQry As String = "SELECT imgName FROM MyImage"
Adpt = New SQLiteDataAdapter(strQry, Conn)
Dim Dt As New DataTable
Adpt.Fill(Dt)
lstImages.DataSource = Nothing
lstImages.Items.Clear()
lstImages.DisplayMember = "imgName"
lstImages.DataSource = Dt
End Sub
Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
With OpenFileDialog1
.InitialDirectory = "C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\"
.Filter = "JPEGs|*.jpg"
.FilterIndex = 1
End With
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
With pctImage
.Image = Image.FromFile(OpenFileDialog1.FileName)
.SizeMode = PictureBoxSizeMode.StretchImage
End With
strFileName = OpenFileDialog1.FileName.ToString
End If
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
Dim cmd As New SQLiteCommand("DELETE FROM MyImage WHERE imgName = '" & lstImages.Text & "'", Conn)
cmd.ExecuteNonQuery()
DisplayInList()
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Try
Dim ms As New MemoryStream
pctImage.Image.Save(ms, Imaging.ImageFormat.Jpeg)
Dim arrImage() As Byte = ms.GetBuffer
ms.Close()
Try
Dim strImageName As String = strFileName.Substring(strFileName.LastIndexOf("\") + "1")
Dim InsQuery As String = "INSERT INTO MyImage (imgName,imgPict) VALUES(@imgName,@imgPict)"
Dim Cmd As New SQLiteCommand(InsQuery, Conn)
Cmd.Parameters.Add("@imgName", DbType.String, 50).Value = strImageName
Cmd.Parameters.AddWithValue("@imgPict", arrImage)
Cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show("Please select Image to Save")
End Try
DisplayInList()
Catch ex As Exception
MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Conn.Open()
If lstImages.Items.Count > 0 Then
lstImages.SetSelected(0, True)
End If
DisplayInList()
End Sub
Private Sub lstImages_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstImages.SelectedValueChanged
Try
Dim Ds As New DataSet
Dim strQry As String = "SELECT * FROM MyImage WHERE imgName = '" & lstImages.Text & "'"
Adpt = New SQLiteDataAdapter(strQry, Conn)
Adpt.Fill(Ds)
If Not Ds.Tables(0).Rows.Count = 0 Then
Dim arrayImage() As Byte = Ds.Tables(0).Rows(0).Item(1)
Dim ms As New MemoryStream(arrayImage)
pctImage.Image = Image.FromStream(ms)
pctImage.SizeMode = PictureBoxSizeMode.StretchImage
Else
pctImage.Image = Nothing
End If
Catch ex As Exception
MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
If Conn.State = ConnectionState.Open Then Conn.Close()
Application.Exit()
End Sub