How to Convert Base64 string to Byte Array using Visual Basic

Introduction

In this article, I will explain you how to convert Base64 string to Byte Array using Visual Basic


techstudy.org

Kindle Paperwhite High Resolution Display Tablet | Rs. 9999

1) When the image upload button clicked, that particular image file is read into a Byte Array with the help of BinaryReader class Object.


2) Now that Byte Array is converted into Base64 string using Convert.ToBase64String method.


3) Now to save Base64 encoded string as Image File, Base64 string is converted back to Byte Array with the help of Convert.FromBase64String function.


4) Lastly, the Byte Array save to your folder as a file using WriteAllBytes method.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
Protected Sub btnUpload_Click(sender As Object, e As EventArgs)
    'Convert Image File to Base64 Encoded string
 
    'Read the uploaded file using BinaryReader and convert it to Byte Array.
    Dim br As New BinaryReader(FileUpload1.PostedFile.InputStream)
    Dim bytes As Byte() = br.ReadBytes(CInt(FileUpload1.PostedFile.InputStream.Length))
 
    'Convert the Byte Array to Base64 Encoded string.
    Dim base64String As String = Convert.ToBase64String(bytes, 0, bytes.Length)
 
    'Save Base64 Encoded string as Image File
 
    'Convert Base64 Encoded string to Byte Array.
    Dim imageBytes As Byte() = Convert.FromBase64String(base64String)
 
    'Save the Byte Array as Image File.
    Dim filePath As String = Server.MapPath("~/Images/" + Path.GetFileName(FileUpload1.PostedFile.FileName))
    File.WriteAllBytes(filePath, imageBytes)
End Sub

     



Related Topics

Responses




Popular Posts

Facebook

Founded in 2016, Tech Study passionately delivers stylish and dynamic innovative information to programmer and technology lovers. http://www.techstudy.org/.