How do I decode a Base64 encoded binary?

Category: System, viewed: 2334 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();
        }
    }
}
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Our Friends

Statistics

Locations of visitors to this page