How to Add and Retrieve Multiple Images in VB.Net

0
89

How to Add and Retrieve Multiple Images in VB.Net

In this tutorial, you will learn how to add and retrieve multiple images in VB.Net.

First, create your project and add these tools on your blank project. (see image below.)

How to Add and Retrieve Multiple Images in VB.Net
How to Add and Retrieve Multiple Images in VB.Net

Then, create a database named retrieveimage then add this code to add image table with its columns.

  1. CREATE TABLE IF NOT EXISTS `image` (
  2.  
  3. `ID` int(10) NOT NULL,
  4.  
  5. `image1` longblob NOT NULL,
  6.  
  7. `image2` longblob NOT NULL,
  8.  
  9. `image3` longblob NOT NULL,
  10.  
  11.  
  12. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Then on our “choose image” button, insert this line of code:

  1. Try
  2. Dim OFD As FileDialog = New OpenFileDialog()
  3. OFD.Filter = "Image File (*.jpg;*)|*.jpg;*"
  4. If OFD.ShowDialog() = DialogResult.OK Then
  5. imgpath = OFD.FileName
  6. PictureBox1.ImageLocation = imgpath
  7. End If
  8. OFD = Nothing
  9. Catch ex As Exception
  10. MsgBox(ex.Message.ToString())
  11. End Try

For the next “choose image” button just change the name of PictureBox depending on its upper picture box name.

Add this line of code at the top of your Form1 code:

  1. Imports MySql.Data.MySqlClient
  2. Dim imgpath As String
  3. Dim arrImage() As Byte
  4. Dim arrImage2() As Byte
  5. Dim arrImage3() As Byte
  6. Dim conn As MySqlConnection
  7. Dim cmd As MySqlCommand
  8. Dim da As MySqlDataAdapter
  9. Dim sql As String
  10. Dim result As Integer

Next, on the “Add Image” button, insert this code:

  1. Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
  2. If TextBox1.Text = "" Then
  3. MsgBox("Input ID!")
  4. ElseIf IsNothing(PictureBox1.Image) Then
  5. MsgBox("Select image 1!")
  6. ElseIf IsNothing(PictureBox2.Image) Then
  7. MsgBox("Select image 2!")
  8. ElseIf IsNothing(PictureBox3.Image) Then
  9. MsgBox("Select image 3!")
  10. Else
  11. Try
  12. Dim mstream As New System.IO.MemoryStream()
  13. PictureBox1.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
  14. arrImage = mstream.GetBuffer()
  15. Dim FileSize As UInt32
  16. FileSize = mstream.Length
  17. mstream.Close()
  18. conn = New MySqlConnection
  19. conn.ConnectionString = "server=localhost; user id=root; password=; database=retrieveimage;"
  20. conn.Open()
  21. sql = "INSERT INTO image (`ID`, `image1`) VALUES (@ID, @Image1);"
  22. cmd = New MySqlCommand
  23. With cmd
  24. .Connection = conn
  25. .CommandText = sql
  26. .Parameters.Clear()
  27. .Parameters.AddWithValue("@ID", TextBox1.Text)
  28. .Parameters.AddWithValue("@Image1", arrImage)
  29. .ExecuteNonQuery()
  30. End With
  31. Catch ex As MySqlException
  32. MsgBox(ex.Message)
  33. Finally
  34. conn.Close()
  35. Try
  36. Dim mstream As New System.IO.MemoryStream()
  37. PictureBox2.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
  38. arrImage2 = mstream.GetBuffer()
  39. Dim FileSize As UInt32
  40. FileSize = mstream.Length
  41. mstream.Close()
  42. conn = New MySqlConnection
  43. conn.ConnectionString = "server=localhost; user id=root; password=; database=retrieveimage;"
  44. conn.Open()
  45. sql = "UPDATE image SET image2 = @Image2 WHERE ID = @ID;"
  46. cmd = New MySqlCommand
  47. With cmd
  48. .Connection = conn
  49. .CommandText = sql
  50. .Parameters.Clear()
  51. .Parameters.AddWithValue("@ID", TextBox1.Text)
  52. .Parameters.AddWithValue("@Image2", arrImage2)
  53. .ExecuteNonQuery()
  54. End With
  55. Catch ex As MySqlException
  56. MsgBox(ex.Message)
  57. Finally
  58. conn.Close()
  59. Try
  60. Dim mstream As New System.IO.MemoryStream()
  61. PictureBox3.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
  62. arrImage3 = mstream.GetBuffer()
  63. Dim FileSize As UInt32
  64. FileSize = mstream.Length
  65. mstream.Close()
  66. conn = New MySqlConnection
  67. conn.ConnectionString = "server=localhost; user id=root; password=; database=retrieveimage;"
  68. conn.Open()
  69. sql = "UPDATE image SET image3 = @Image3 WHERE ID = @ID;"
  70. With cmd
  71. .Connection = conn
  72. .CommandText = sql
  73. .Parameters.Clear()
  74. .Parameters.AddWithValue("@ID", TextBox1.Text)
  75. .Parameters.AddWithValue("@Image3", arrImage3)
  76. result = .ExecuteNonQuery()
  77. End With
  78. Catch ex As MySqlException
  79. MsgBox(ex.Message)
  80. Finally
  81. conn.Close()
  82. If result = 0 Then
  83. MsgBox("Error in registering!")
  84. Else
  85. MsgBox("Successfully added images!")
  86. End If
  87. TextBox1.Text = ""
  88. PictureBox1.Image = Nothing
  89. PictureBox2.Image = Nothing
  90. PictureBox3.Image = Nothing
  91. End Try
  92. End Try
  93. End Try
  94. End If
  95. End Sub

To retrieve our images from the database, double click the “Retrieve Image” button and insert this code.

  1. Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
  2. If TextBox1.Text = "" Then
  3. MsgBox("Input ID for retrieval!")
  4. Else
  5. Try
  6. conn = New MySqlConnection
  7. conn.ConnectionString = "server=localhost; user id=root; password=; database=retrieveimage;"
  8. conn.Open()
  9. sql = "SELECT * FROM image WHERE ID = @ID;"
  10. cmd = New MySqlCommand
  11. With cmd
  12. .Connection = conn
  13. .CommandText = sql
  14. .Parameters.Clear()
  15. .Parameters.AddWithValue("@ID", TextBox1.Text)
  16. .ExecuteNonQuery()
  17. End With
  18. Dim arrImage(), arrImage2(), arrImage3() As Byte
  19. Dim dt As New DataTable
  20. da = New MySqlDataAdapter
  21. da.SelectCommand = cmd
  22. da.Fill(dt)
  23. If dt.Rows.Count > 0 Then
  24. MsgBox("Showing results!")
  25. arrImage = dt.Rows(0).Item(1)
  26. arrImage2 = dt.Rows(0).Item(2)
  27. arrImage3 = dt.Rows(0).Item(3)
  28. Dim mstream As New System.IO.MemoryStream(arrImage)
  29. Dim mstream2 As New System.IO.MemoryStream(arrImage2)
  30. Dim mstream3 As New System.IO.MemoryStream(arrImage3)
  31. PictureBox1.Image = Image.FromStream(mstream)
  32. PictureBox2.Image = Image.FromStream(mstream2)
  33. PictureBox3.Image = Image.FromStream(mstream3)
  34. Else
  35. MsgBox("No results!")
  36. End If
  37. Catch ex As MySqlException
  38. MsgBox(ex.Message)
  39. Finally
  40. conn.Close()
  41. da.Dispose()
  42. End Try
  43. End If
  44. End Sub

Success! You have learned How to Add and Retrieve Multiple Images in VB.Net!

RAR Extraction Password:

Password: luffypirates

For questions or any other concerns or thesis/capstone creation with documentation, you can contact me through the following:

E-Mail: [email protected]

Facebook: facebook.com/kirk.lavapiez

Contact No.: +639771069640

To download the sample project, click here.

Ian Hero L. Lavapiez

BSIT Graduate

System Analyst and Developer

Related topic(s) that you may like:

 

Facebook Comments
(Visited 99 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here