How do I decode a Base64 encoded binary?
Category: System, viewed: 6K time(s).
This examples demonstrate how to decode an encoded Base64 binary information. We read the encoded string from a text file and create an binary file (image file) from this information.
using System;
using System.IO;
namespace Kodecsharp.Example.System
{
public class DecodeBase64Binary
{
public static void Main(string[] args)
{
//
// Read the contents of Encoded Bitmap from the text file.
//
TextReader reader = new StreamReader(@"C:\Water lilies.txt");
String imageString = reader.ReadLine();
//
// Decode the Base64-Encoded binary whic previously read from the
// text file above.
//
byte[] imageBytes = Convert.FromBase64String(imageString);
//
// Create a new image file.
//
FileStream fs = new FileStream(@"C:\New Water lilies.jpg", FileMode.Create, FileAccess.Write);
BinaryWriter writer = new BinaryWriter(fs);
try {
for (int i = 0; i < imageBytes.Length; i++)
{
writer.Write(imageBytes[i]);
}
} finally {
writer.Close();
fs.Close();
}
Console.ReadLine();
}
}
}