Home
VB.NET
ToBase64String Example
Updated Nov 29, 2023
Dot Net Perls
ToBase64String. Sometimes it is helpful to have a text string representation of a binary file like an image. This text could be stored in a database, or directly on a web page.
With the Convert class, we have many helpful conversion utility methods. And ToBase64String helps us convert a byte array into a string with base-64 encoding.
Byte Array
To begin, we include the System.IO namespace—this is for the File.ReadAllBytes function. The Convert class is part of the System namespace, which is part of every VB.NET program.
File
Step 1 We specify the path of a binary file—in this program, we are using an image. Then we read in the file with File.ReadAllBytes.
Step 2 We invoke the ToBase64String function, which receives a byte array and returns a String.
Imports System.IO Module Module1 Sub Main() ' Step 1: specify the path of the image, and read all bytes from it. Dim image = "programs/adobe.png" Dim data = File.ReadAllBytes(image) ' Step 2: Call Convert.ToBase64String function. Dim result = Convert.ToBase64String(data) Console.WriteLine(result) End Sub End Module
iVBORw0KGgoAAA ... AABJRU5ErkJggg==
Data URIs. On web pages (HTML documents) we can specify data URIs in the place of external HTTPS links. And the data returned by ToBase64String works inside these data URIs.
Summary. With .NET, we have many powerful features built into our languages, including base-64 encoding. There is no need in VB.NET to use an external library to encode images into text strings.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Nov 29, 2023 (new).
Home
Changes
© 2007-2025 Sam Allen