C# How to: Decoding/Converting Base64 strings to Bitmap images

Article purpose

This details how to decode or convert encoded back into by means of the class.

Note: This article is an update that builds upon the article:

Sample source code

This is accompanied by a sample source code Visual Studio project which is available for download .

Images as Base64 strings

From :

Base64 is a group of similar encoding schemes that represent in an ASCII string format by translating it into a -64 representation. The Base64 term originates from a specific .

Base64 encoding schemes are commonly used when there is a need to encode binary data that need to be stored and transferred over media that are designed to deal with textual data. This is to ensure that the data remain intact without modification during transport. Base64 is commonly used in a number of applications including email via MIME, and storing complex data in XML.

From the definition quoted above the need for encoding becomes more clear. From :

The base-64 digits in ascending order from zero are the uppercase characters "A" to "Z", the lowercase characters "a" to "z", the numerals "0" to "9", and the symbols "+" and "/". The valueless character, "=", is used for trailing padding.

encoding allows developers to expose without potentially encountering conflicts in regards to the transfer medium. encoded serves ideally when performing data transfer operations using platforms such as html, json, rest, xml, email.

A common implementation of encoding can be found when transferring data. This details how to convert/decode a back into a Bitmap .

Base64 String to Bitmap decoding implemented as an extension method

The code snippet listed below details the Base64StringToBitmap targeting the .

public static Bitmap Base64StringToBitmap(this string
                                           base64String)
{
    Bitmap bmpReturn = null;

byte[] byteBuffer = Convert.FromBase64String(base64String); MemoryStream memoryStream = new MemoryStream(byteBuffer);
memoryStream.Position = 0;
bmpReturn = (Bitmap)Bitmap.FromStream(memoryStream);
memoryStream.Close(); memoryStream = null; byteBuffer = null;
return bmpReturn; }

The parameter is first converted to a array by invoking the method. Next we create a against the resulting array, which serves as a parameter to the class’ static method.

The implementation

The Base64StringToBitmap is implemented in a console based application. The creates a instance from the local file system, which is then converted to a .

An article detailing how to convert to encoded can be found here:

– The you are currently reading was published as a follow up .

Next the newly created is converted back to a by invoking the Base64StringToBitmap . In order to test if the was decoded successfully the is saved to the local file system and displayed in the default installed application associated with png . The implementation as follows:

static void Main(string[] args)
{
    StreamReader streamReader = new StreamReader(
                                   "NavForward.png");

Bitmap bmp = new Bitmap(streamReader.BaseStream); streamReader.Close();
string base64ImageString = bmp.ToBase64String( ImageFormat.Png);
Console.WriteLine(base64ImageString);
Bitmap bmpFromString = base64ImageString.Base64StringToBitmap();
bmpFromString.Save("FromBase64String.png", ImageFormat.Png);
Process.Start("FromBase64String.png");
Console.ReadKey(); }

Related Articles and Feedback

Feedback and questions are always encouraged. If you know of an alternative implementation or have ideas on a more efficient implementation please share in the comments section.

Dewald Esterhuizen

I’ve published a number of articles related to imaging and images of which you can find URL links here:

0 Responses to “C# How to: Decoding/Converting Base64 strings to Bitmap images”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




Dewald Esterhuizen

Blog Stats

  • 472,611 hits

Enter your email address to follow and receive notifications of new posts by email.

Join 212 other followers

Archives

Twitter feed


%d bloggers like this: