Convert image to base 64 string and base 64 string to image in c#

Converting and image to base 64 from an image or from a path or converting a base 64 string to an image is quite easy. We will use MemoryStream to convert them. We will use two simple method one to convert the image to base 64 string and other to convert the base 64 string to image. Image can be saved wherever you want or show them anywhere on the page

Code to convert the image into base 64 string by passing the image page, if you are using asp.net or web page then you need to use Server.MapPath to get the correct image path than absolute path

public String ConvertImageToBase64String(String imagePath)
{
    using (var img = System.Drawing.Image.FromFile(imagePath))
    {
        using (var memStream = new MemoryStream())
        {
            img.Save(memStream, img.RawFormat);
            byte[] imageBytes = memStream.ToArray();

            // Convert byte[] to Base64 String
            string base64String = Convert.ToBase64String(imageBytes);
            return base64String;
        }
    }
}

First we convert the image path into image, then save this image into memory and finally converted into base 64 string.

Now we will write the code to convert the base 64 string into image, we will pass base 64 string as parameter and then convert it into bytes and then to memory string and finally to image.

public System.Drawing.Image Base64StringToImage(string base64String)
{
    byte[] imageBytes = Convert.FromBase64String(base64String);
    var memStream = new MemoryStream(imageBytes, 0, imageBytes.Length);

    memStream.Write(imageBytes, 0, imageBytes.Length);
    var image = System.Drawing.Image.FromStream(memStream);
    return image;
}

If you want to save this image in any folder simple use image.save method to save it.

Hamden Process manager with a reputed organization, Fond of learning new features and technology related to C#, ASP.Net, SQL Server, MVC etc.I like to help others, if I can
  • base64string
  • memorystream
  • c#
By Hamden On 01 Jan, 15  Viewed: 1,135

Other blogs you may like

default value for different data type in C#

In C# there are different data type and they use some default value when we declare a variable. When we define a variable of type int or Int32 say int score; so what is the value of score, will it be null or zero, in the same way if we create a variable of type string/String what value it holds... By Ali Adravi   On 27 Mar 2013  Viewed: 1,263

enum in C# and conversion

An enumeration type (also known an enumeration or an enum) provides an efficient way to define a set of named integral constants that may be assigned to a variable to make programming clear, understandable and manageable. There are ways of using, like how to use enum in switch and case statement,... By Hamden   On 27 Mar 2013  Viewed: 2,215

Dynamic vs var in C# with example

When we have var which dynamically holds any type or value so why we need dynamic type, what are the differences and situations where we can use dynamic rather than var, there were some question in my mind so I explored more which I want to share here. There are two things, static type and... By Nathan Armour   On 26 Mar 2013  Viewed: 1,828

Array in C# - single, multiple and jagged array

An array can be initialized in several ways, it can store a single value or series of values, every element must be set of same type, C# supports single-dimensional arrays, multidimensional arrays (also call rectangular array) and array-of-arrays (referred jagged arrays), let’s discuss features of... By Ali Adravi   On 24 Mar 2013  Viewed: 2,054

XmlDocument Vs XDocument and their usage

When there was XmlDocument why we need XDocument, is there any better feature available with XDocument, answer is yes, XDocument has a number of benefits, easy to create, easy to update, easy to search, ability to use LINQ to XML, cleaner object model etc. In this post we will see some examples... By Ali Adravi   On 16 Mar 2013  Viewed: 972