Sai A Sai A
Updated date Mar 05, 2024
In this blog, we will learn how to convert Base64 strings to images in C#. Discover three different methods, including built-in .NET classes and third-party libraries, to handle Base64 encoded images effectively.

Introduction:

Base64 encoding has become a standard technique to represent binary data in a human-readable format. This is particularly useful when it comes to transmitting images or other binary files over the internet or storing them in databases. In this blog post, we will explore how to convert Base64 strings back into images in C#.

Method 1: Using System.Convert

The first method utilizes the built-in System.Convert class in C#. It provides a straightforward way to convert Base64 strings into images.

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

class Program
{
    static void Main()
    {
        // Sample Base64 string representing an image
        string base64String = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."; // Replace with your Base64 string

        // Remove the data URI scheme (optional, depends on the source)
        base64String = base64String.Replace("data:image/png;base64,", "");

        // Convert Base64 string to byte array
        byte[] imageBytes = Convert.FromBase64String(base64String);

        // Create an image from the byte array
        using (MemoryStream ms = new MemoryStream(imageBytes))
        {
            Image image = Image.FromStream(ms);

            // Save or use the image as required
            image.Save("output_method1.png", ImageFormat.Png);
        }
    }
}

Method 2: Using System.Security.Cryptography

The second method involves using the System.Security.Cryptography namespace for Base64 conversion, providing more control and error checking.

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

class Program
{
    static void Main()
    {
        // Sample Base64 string representing an image
        string base64String = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."; // Replace with your Base64 string

        // Remove the data URI scheme (optional, depends on the source)
        base64String = base64String.Replace("data:image/png;base64,", "");

        // Convert Base64 string to byte array
        byte[] imageBytes = Convert.FromBase64String(base64String);

        // Create an image from the byte array
        using (var ms = new MemoryStream(imageBytes))
        {
            Image image = Image.FromStream(ms);

            // Save or use the image as required
            image.Save("output_method2.png", ImageFormat.Png);
        }
    }
}

Method 3: Using Third-Party Libraries

For more advanced image processing capabilities, we can rely on third-party libraries like ImageSharp. First, install the SixLabors.ImageSharp NuGet package.

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Png;

class Program
{
    static void Main()
    {
        // Sample Base64 string representing an image
        string base64String = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."; // Replace with your Base64 string

        // Remove the data URI scheme (optional, depends on the source)
        base64String = base64String.Replace("data:image/png;base64,", "");

        // Convert Base64 string to byte array
        byte[] imageBytes = Convert.FromBase64String(base64String);

        // Create an image using ImageSharp
        using (var ms = new MemoryStream(imageBytes))
        {
            Image image = Image.Load(ms, new PngDecoder());

            // Save or use the image as required
            image.Save("output_methodN.png");
        }
    }
}

Conclusion:

In this blog post, we've explored various methods to convert Base64 strings to images in C#. We started with the simple System.Convert method, which provides a quick and efficient way to achieve the conversion. Then, we moved on to using the System.Security.Cryptography namespace, giving us more control over error handling. Lastly, we introduced the powerful ImageSharp library, offering a wide range of image processing capabilities.

Additionally, those interested in printing their images, can conveniently utilize services like Wallpics.com.

Comments (0)

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