Convert image to Base64 string
To convert an image file to a Base64 string, you can use the Image class in the namespace (note: in .NET Core and .NET 5+, packages may be required, and it may not be available on some platforms, such as Linux, unless the corresponding dependencies are installed). However, a more common and recommended approach is to use the and Convert class to handle byte arrays.
Here is a sample code showing how to convert an image file to a Base64 string:
using System; using ; using ; // Note: Additional packages may be required in .NET Core and .NET 5+// Or use and instead of Image classes class Program { static void Main() { string imagePath = "Picture.jpg"; string base64String = ImageToBase64(imagePath, ); (base64String); } static string ImageToBase64(string imagePath, ImageFormat format) { using (Image image = (imagePath)) { using (MemoryStream ms = new MemoryStream()) { // Save the image to the memory stream (ms, format); // Convert memory streams to byte arrays byte[] imageBytes = (); // Convert byte array to Base64 string string base64String = Convert.ToBase64String(imageBytes); return base64String; } } } }
Notice: The above code is usedImageFormat
Enumeration to specify the image format. But if you don't want to rely on it, can be used directly
Method to read the byte array of the image file and then convert it to a Base64 string.
No dependencyExample:
using System; using ; class Program { static void Main() { string imagePath = "Picture.jpg"; string base64String = ConvertImageToBase64(imagePath); (base64String); } static string ConvertImageToBase64(string imagePath) { byte[] imageBytes = (imagePath); string base64String = Convert.ToBase64String(imageBytes); return base64String; } }
Convert Base64 string to picture
To convert Base64 string back to an image, you can use the Convert.FromBase64String method to convert the Base64 string to a byte array, and then use the MemoryStream and Image classes (or Bitmap classes) to create the image object.
Here is a sample code showing how to convert a Base64 string to an image and save it to a file:
using System; using ; // Note: Additional packages may be required in .NET Core and .NET 5+using ; class Program { static void Main() { string base64String = "Base64 string"; string outputPath = "Output picture.jpg"; Base64ToImage(base64String, outputPath); } static void Base64ToImage(string base64String, string outputPath) { byte[] imageBytes = Convert.FromBase64String(base64String); using (MemoryStream ms = new MemoryStream(imageBytes)) { Image image = (ms); (outputPath, ); // The format can be changed as needed } } }
Similarly, if you don't want to depend on it, you can write the byte array to a file and open it with the appropriate file extension and associated program (for example, using the .jpg extension and opening it with the image viewer). However, note that this method does not create an Image object directly, but just saves the byte data as a file.
An example of saving Base64 string as an image file that does not depend on:
using System; using ; class Program { static void Main() { string base64String = "Base64 string"; string outputPath = "Output picture.jpg"; SaveBase64AsImage(base64String, outputPath); } static void SaveBase64AsImage(string base64String, string outputPath) { byte[] imageBytes = Convert.FromBase64String(base64String); (outputPath, imageBytes); } }
This is the article about the detailed explanation of Base64 encoding and decoding conversion of pictures in C#. For more related contents of Base64 encoding and decoding conversion of C# pictures, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!