Mohanapriya R Mohanapriya R
Updated date May 29, 2024
In this article, we will learn how to convert bitmap images to strings in C# using a simple yet powerful approach.

Converting Bitmap to Strings in C#:

To convert a bitmap image to strings in C#, we will be using the Convert.ToBase64String method, which encodes the bitmap image as a base64 string.

using System;
using System.Drawing;
using System.IO;

class Program
{
    static void Main()
    {
        // Load the bitmap image
        Bitmap bitmap = new Bitmap("techiehook-logo.bmp");

        // Convert the bitmap to base64 string
        string base64String = ConvertBitmapToString(bitmap);

        // Display the result
        Console.WriteLine("Base64 String Representation of the Bitmap:");
        Console.WriteLine(base64String);
        Console.ReadKey();
    }

    static string ConvertBitmapToString(Bitmap bitmap)
    {
        // Convert the bitmap to a byte array
        MemoryStream ms = new MemoryStream();
        bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
        byte[] byteImage = ms.ToArray();

        // Convert the byte array to a base64 string
        string base64String = Convert.ToBase64String(byteImage);

        return base64String;
    }
}

Output:

Comments (0)

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