How to Convert Image to Base64 in VB.Net

2
2539

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.
    [vbnet] Imports System.IO
    Imports System.Drawing.Imaging
    Imports System.Data.OleDb
    [/vbnet]
  • Add this Public Sub.
    [vbnet] Public Class Main
    Dim Con As OleDbConnection = New OleDbConnection(“Provider=Microsoft.ACE.OLEDB.12.0;Data Source=” & Application.StartupPath & “\Image.accdb”)
    Dim Cmd As New OleDbCommand
    Dim Da As New OleDbDataAdapterPrivate ImagePath As String = “”

    [/vbnet]
  • Add this following Functions
    [vbnet] Public Function ImageToBase64(ByVal image As Image, ByVal format As System.Drawing.Imaging.ImageFormat) As String
    Using ms As New MemoryStream()
    ‘ Convert Image to byte[] image.Save(ms, format)
    Dim imageBytes As Byte() = ms.ToArray()’ Convert byte[] to Base64 String
    Dim base64String As String = Convert.ToBase64String(imageBytes)
    Return base64String
    End Using
    End Function

    Public Function Base64ToImage(ByVal base64String As String) As Image
    ‘ Convert Base64 String to byte[] Dim imageBytes As Byte() = Convert.FromBase64String(base64String)
    Dim ms As New MemoryStream(imageBytes, 0, imageBytes.Length)

    ‘ Convert byte[] to Image
    ms.Write(imageBytes, 0, imageBytes.Length)
    Dim ConvertedBase64Image As Image = Image.FromStream(ms, True)
    Return ConvertedBase64Image
    End Function

    [/vbnet]
  • Add this code to the Add new button.
    [vbnet] Private Sub BtnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAddNew.Click
    TxtBase64.Clear()
    PicImage.Image = Nothing
    End Sub
    [/vbnet]
  • Add this code to the Image to Base64 button.
    [vbnet] Private Sub BtnImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnConvertToBase64.Click
    OfdImage.ShowDialog()
    If My.Computer.FileSystem.FileExists(ImagePath) Then
    Dim ImageToConvert As Bitmap = Bitmap.FromFile(ImagePath)
    TxtBase64.Text = ImageToBase64(ImageToConvert, ImageFormat.Png)
    End IfTry
    Con.Open()
    Cmd = New OleDbCommand(String.Format(“INSERT INTO Images ( DateCreated, ImageCode) VALUES(‘{0}’, ‘{1}’)”, Date.Now, TxtBase64.Text), Con)
    Dim Result = Cmd.ExecuteNonQuery()
    If Result > 0 Then
    MessageBox.Show(“Added image successfully!”)
    End If
    Con.Close()

    TxtBase64.Clear()
    Catch ex As Exception
    End Try

    End Sub

    [/vbnet]
  • Add this code to the Base64 to Image button.
    [vbnet] Private Sub BtnConvertToImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnConvertToImage.Click
    PicImage.Image = Base64ToImage(TxtBase64.Text)
    End Sub
    [/vbnet]
  • 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

2 COMMENTS

  1. It works great the Code. How can I add a save feature after the image has been decoded? I try to right click to save the decoded image it but it dont give me a option.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.