Mohanapriya R Mohanapriya R
Updated date Jun 11, 2024
In this article, we will learn how to easily convert images to Base64 strings in C#.
  • 2.9k
  • 0
  • 0

Converting Image to Base64 String in C#

In this program, we will explore how to convert an image to a Base64 string using C#. 

using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        // Path to the image file
        string imagePath = "mountains.jpg";

        // Read the image file into a byte array
        byte[] imageBytes = File.ReadAllBytes(imagePath);

        // Convert the byte array to a Base64 string
        string base64String = Convert.ToBase64String(imageBytes);

        // Output the Base64 string
        Console.WriteLine(base64String);
    }
}

In this example, we have specified the path to the image file (e.g., mountains.jpg) Then, we used File.ReadAllBytes() to read the contents of the image file into a byte array. Next, we used Convert.ToBase64String() to convert the byte array to a Base64 string. Finally, we output the resulting Base64 string to the console.

Output:

Once you run the program you can see the output will be the Base64 representation of the image.

/9j/4AAQSkZJRgABAQEAYABgAAD/4QBARXhpZgAATU0AKgAAAAgAA1EQAAEAAAABAQAAAFERAAQAAAABAAABAFESAAQAAAABAAABAFeSAAMAAAABAAIAAAEyAAIAAAAUAAABOTgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

Comments (0)

There are no comments. Be the first to comment!!!