This blog provides code examples for converting images to Base64 and Base64 to images. Using these examples, you can convert PNG to Base64, Base64 to PNG, encode images in Base64, and convert Base64 strings back to images in C#.
Convert Image to Base64 and Base64 to Image in C#
Getting Started
Working with images in applications often requires converting them to a different format for storage or transmission. One popular format is Base64, which allows binary data to be represented as plain text. This is especially useful for embedding images in JSON
, XML
, or HTML
.
A Base64 string is a way of encoding binary data (like images, files, or bytes) into text using only 64 ASCII characters. It’s commonly used when you need to send binary data over mediums that only support text—like JSON
, XML
, or email.
Code example
Here the below examples describes different ways to convert image to base64 string and vice versa.
Convert Image to Base64
class Program
{
static void Main()
{
string imagePath = "path/to/your/image.jpg"; // Replace with your actual image path
string base64String = ImageToBase64(imagePath);
Console.WriteLine(base64String);
}
static string ImageToBase64(string imagePath)
{
byte[] imageBytes = File.ReadAllBytes(imagePath);
string base64String = Convert.ToBase64String(imageBytes);
return base64String;
}
}
Convert Image to Base64 with System.Drawing
If you want to control the format (JPEG, PNG, etc.): public string ImageToBase64()
{
string path = "D:\\SampleImage.jpg";
using(System.Drawing.Image image = System.Drawing.Image.FromFile(path))
{
using(MemoryStream m = new MemoryStream())
{
image.Save(m, ImageFormat.Jpeg);
byte[] imageBytes = m.ToArray();
base64String = Convert.ToBase64String(imageBytes);
return base64String;
}
}
}
In the above code snippet, the image is read using the FormFile method of Image class, then using the Save method the image is saved into the system memory as JPEG format with help of MemoryStream class. The raw formatted memory again converted into bytes, later using convert to base65String method converted the bytes to base64.
Check the result in Base64 to Image online Converter Tool
Convert Base64 to Image
The reverse activity of the above section Convert image to base64 is done here, first, the string is converted into bytes using Convert.FromBase64String method then it stored into memory. Later that memory is converted into an image using the FromStream method of Image class. See the below example for more details.
Basic Example using System;
using System.IO;
namespace imagebase64
{
internal class Program
{
static void Main(string[] args)
{
byte[] imageBytes = Convert.FromBase64String("base64String");
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
ms.Write(imageBytes, 0, imageBytes.Length);
System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
image.Save("base64_decode_image");
}
}
}
A Base64-decoded image can be converted to various formats using the ImageFormat
class from the System.Drawing.Imaging.ImageFormat
namespace.For example, you can convert Base64 to PNG, JPG, JPEG, GIF, WebP, and more. See the below examples.
byte[] imageBytes = Convert.FromBase64String("base64String");
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
ms.Write(imageBytes, 0, imageBytes.Length);
System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
image.Save("base64decodeimage",ImageFormat.Png);
Base64 to JPG or JPEG
byte[] imageBytes = Convert.FromBase64String("base64String");
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
ms.Write(imageBytes, 0, imageBytes.Length);
System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
image.Save("base64decodeimage",ImageFormat.Jpeg);
Base64 to GIF
byte[] imageBytes = Convert.FromBase64String("base64String");
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
ms.Write(imageBytes, 0, imageBytes.Length);
System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
image.Save("base64decodeimage",ImageFormat.Gif);
Summary
In the above of this blog, we saw how to convert image to base64 and base64 to image using C#. I hope you have enjoyed it a lot.
Thanks