How to Convert Image to Base64 in VB.Net


This Tutorial is all about How to Convert Image to Base64 in VB.Net. In this tutorial you will learn about Convert Image to Base64 in VB.Net. So lets get Started:

  • First is open the Visual Basic, Select File on the menu, then click New and create a new project.

  • Then a New Project Dialog will appear. You can rename your project, depending on what you like to name it. After that click OK

  • Design your form like this just like what I’ve shown you below.
    Add a Button, Textbox and a Picturebox.
  • Add this following Declarations.
    1. Imports System.IO
    2. Imports System.Drawing.Imaging
    3. Imports System.Data.OleDb
  • Add this Public Sub.
    1. Public Class Main
    2. Dim Con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\Image.accdb")
    3. Dim Cmd As New OleDbCommand
    4. Dim Da As New OleDbDataAdapterPrivate ImagePath As String = ""
  • Add this following Functions
    1. Public Function ImageToBase64(ByVal image As Image, ByVal format As System.Drawing.Imaging.ImageFormat) As String
    2. Using ms As New MemoryStream()
    3. ' Convert Image to byte[]
    4. image.Save(ms, format)
    5. Dim imageBytes As Byte() = ms.ToArray()' Convert byte[] to Base64 String
    6. Dim base64String As String = Convert.ToBase64String(imageBytes)
    7. Return base64String
    8. End Using
    9. End Function
    11. Public Function Base64ToImage(ByVal base64String As String) As Image
    12. ' Convert Base64 String to byte[]
    13. Dim imageBytes As Byte() = Convert.FromBase64String(base64String)
    14. Dim ms As New MemoryStream(imageBytes, 0, imageBytes.Length)
    16. ' Convert byte[] to Image
    17. ms.Write(imageBytes, 0, imageBytes.Length)
    18. Dim ConvertedBase64Image As Image = Image.FromStream(ms, True)
    19. Return ConvertedBase64Image
    20. End Function
  • Add this code to the Add new button.
    1. Private Sub BtnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAddNew.Click
    2. TxtBase64.Clear()
    3. PicImage.Image = Nothing
    4. End Sub
  • Add this code to the Image to Base64 button.
    1. Private Sub BtnImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnConvertToBase64.Click
    2. OfdImage.ShowDialog()
    3. If My.Computer.FileSystem.FileExists(ImagePath) Then
    4. Dim ImageToConvert As Bitmap = Bitmap.FromFile(ImagePath)
    5. TxtBase64.Text = ImageToBase64(ImageToConvert, ImageFormat.Png)
    6. End IfTry
    7. Con.Open()
    8. Cmd = New OleDbCommand(String.Format("INSERT INTO Images ( DateCreated, ImageCode) VALUES('{0}', '{1}')", Date.Now, TxtBase64.Text), Con)
    9. Dim Result = Cmd.ExecuteNonQuery()
    10. If Result > 0 Then
    11. MessageBox.Show("Added image successfully!")
    12. End If
    13. Con.Close()
    15. TxtBase64.Clear()
    16. Catch ex As Exception
    17. End Try
    19. End Sub
  • Add this code to the Base64 to Image button.
    1. Private Sub BtnConvertToImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnConvertToImage.Click
    2. PicImage.Image = Base64ToImage(TxtBase64.Text)
    3. End Sub
  • Finally Click F5 to run program.

If you have any comments or suggestion about on How to Convert Image to Base64 in VB.Net, Please Feel Free to contact our webpage.

Download How to Convert Image to Base64 in VB.Net Code Here

Other articles you might read also:



Facebook Comments


Please enter your comment!
Please enter your name here