Article purpose
This article details how to decode or convert Base64 encoded strings back into Bitmap images by means of extending the string class.
Note: This article is an update that builds upon the article: C# How to: Encoding Bitmaps to Base64 strings
Sample source code
This article is accompanied by a sample source code Visual Studio project which is available for download here.
Images as Base64 strings
From Wikipedia:
Base64 is a group of similar encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. The Base64 term originates from a specific MIME content transfer encoding.
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 Base64 encoding becomes more clear. From MSDN documentation:
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.
Base64 encoding allows developers to expose binary data without potentially encountering conflicts in regards to the transfer medium. Base64 encoded binary data serves ideally when performing data transfer operations using platforms such as html, json, rest, xml, email.
A common implementation of Base64 encoding can be found when transferring image data. This article details how to convert/decode a Base64 string back into a Bitmap image.
Base64 String to Bitmap decoding implemented as an extension method
The code snippet listed below details the Base64StringToBitmap extension method targeting the String class.
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 Base64 string parameter is first converted to a byte array by invoking the Convert.FromBase64String method. Next we create a MemoryStream against the resulting byte array, which serves as a parameter to the Bitmap class’ FromStream static method.
The implementation
The Base64StringToBitmap extension method is implemented in a console based application. The sample source code creates a Bitmap instance from the local file system, which is then converted to a Base64 string.
An article detailing how to convert Bitmaps to Base64 encoded strings can be found here:
C# How to: Encoding Bitmaps to Base64 strings – The article you are currently reading was published as a follow up article.
Next the newly created Base64 string is converted back to a Bitmap image by invoking the Base64StringToBitmap extension method. In order to test if the Base64 string was decoded successfully the Bitmap image is saved to the local file system and displayed in the default installed application associated with png images. 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.
I’ve published a number of articles related to imaging and images of which you can find URL links here:
- C# How to: Image filtering by directly manipulating Pixel ARGB values
- C# How to: Image filtering implemented using a ColorMatrix
- C# How to: Blending Bitmap images using colour filters
- C# How to: Bitmap Colour Substitution implementing thresholds
- C# How to: Generating Icons from Images
- C# How to: Swapping Bitmap ARGB Colour Channels
- C# How to: Bitmap Pixel manipulation using LINQ Queries
- C# How to: Linq to Bitmaps – Partial Colour Inversion
- C# How to: Bitmap Colour Balance
- C# How to: Bi-tonal Bitmaps
- C# How to: Bitmap Colour Tint
- C# How to: Bitmap Colour Shading
- C# How to: Image Solarise
- C# How to: Image Contrast
- C# How to: Bitwise Bitmap Blending
- C# How to: Image Arithmetic
- C# How to: Image Convolution
- C# How to: Image Edge Detection
- C# How to: Difference Of Gaussians
- C# How to: Image Median Filter
- C# How to: Image Unsharp Mask
- C# How to: Image Colour Average
- C# How to: Image Erosion and Dilation
- C# How to: Morphological Edge Detection
- C# How to: Boolean Edge Detection
- C# How to: Gradient Based Edge Detection
- C# How to: Sharpen Edge Detection
- C# How to: Image Cartoon Effect
- C# How to: Calculating Gaussian Kernels
- C# How to: Image Blur
- C# How to: Image Transform Rotate
- C# How to: Image Transform Shear
- C# How to: Compass Edge Detection
- C# How to: Oil Painting and Cartoon Filter
- C# How to: Stained Glass Image Filter
0 Responses to “C# How to: Decoding/Converting Base64 strings to Bitmap images”