Base64 String to Image and Image to Base64 String

how we can convert an image into a base64 string and base64 string back to image

In this post I am going show how we can convert Base64 string to Image and base64 string to image. it is very easy in C#.net. using this below code we can do that.

Base64 string

E.g. :-

iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAEZ0lEQVR4nO3cPXLcNhgG4O8OaXIONz5IJl0OwzanSZdjJJVb+wr2eKJJ7FhKIWHEpXcZkgCWIPE8M1vIM+JivXihDz9kBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAcHQfI+Lp5fUtIn7btznQjs/xGo7x6/c9GwWtSIH4NSJ+jMuQQPemYXi88m/QrRSGNy8//xmX8xHo2g9xGYY/4rLM+mendkEzUhg+xGuJpdSCkWsrWQICIx/ChB1W+bp3AwAAAGCOlR1O5bHw9QSEUyndoQWEU/k3yh6+ExBOJ+0Kl9BjQEr+/9Ggh3j+gh8KXEtAOKV0SrXEdXrrLN/i+TO/3bsh1JNGwaHQdb7kNuhA3ka5AYZGDVHmSx7fM9ETZVYHSnzJvQYkPQHl894NoZ5US+fo+T6JHgeGrqSHpH3MuMY4ICVWxY6k14GhK7mbhuOA9Daamod0ILdjTx9s0JMeP3N3cr/k9PvpCEtPPsXzZ/60d0Oop1RAIl6D0pNUXv61d0Ooo3RAepuHvI/vn8jIiZQMSK81+Zvoc3DogoCUkfaUeisxT69kQL5kXmvp+yWtPafKvsgJlQxIRL2Di1/jsgO2+DjQtJLnr8iJ1AhI6Vo8dbxpO1sMSWvtIdMRAjLX6Vo7C5bCbMJ+EqUDUnqivmTy21pIxpunHFzpgKSJ+rucRo0sHY1bO3Y/Pp82fXEgpQMSUe6pKWvunf8l2js0+TTzEpaDqBWQEuXO2rb9HZedsFVHaCMvagSkxI1Yt669hA5IMbkBudYR30WZ/ZCtnXy8LKyMIUtOQOY2xnI75xB5c5lpzQ+bjFd/HuO5Qw4rf/ea3I6ZyrSlbbn1+62tbnEw4z2EWystt15LNvBy27XVz3H9M+UStM5MO+IQr6PvktfS6+a2a4vxqtZPUSYkSrbO1DqinruSVapdKRhp5E8dfOtJ4J6P9Hep1hee+0ihku2aHld5mvy8tD2tHWnhDmqOiDmrUKXbNe3YSzv6+Ji9cHSodkC2TmprtGu8+DBdiLhWcgkFVQOSSpstT1ysdfPR/y06TEup1u5a5M5qTzpzRuCao/f0cyuluKp2QNKJ3C2l1q07CUuwGsUi9+go413ttab3opfirwSL3GskzbnLLvfYyZSHK7DYPUuNraP2EHlLxlPKKxa7Z2fJea8UriGzDbVKNk7qngHJeeLH+FzVY2xbOrYTzmr3Ljdy5iJDXHbyFJZhxfs6jcsqe9TjJUbxh7gelrlj+cLBansEpOR7DrHseL4dcTY5ekCgKgGBGXt01tIbf1DNHgEZouzGH1SzV7lTauMPqtorIOONP39JaNaeE+Yh8jYOoboWjl600Ab4zhBKHLjJcivMsGEHMwQEZggIzBAQmGF5FW4YwhIv3JTKq2HndkBznIGCGeYeAAAAAAAAAAAAAAAAAAAAAAAA/fgPhc2nXMjQCzsAAAAASUVORK5CYII=

See the out put image of the string.
Sorry it will work good in Firefox

<img alt=”Base64 string” src=”data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAEZ0lEQVR4nO3cPXLcNhgG4O8OaXIONz5IJl0OwzanSZdjJJVb+wr2eKJJ7FhKIWHEpXcZkgCWIPE8M1vIM+JivXihDz9kBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAcHQfI+Lp5fUtIn7btznQjs/xGo7x6/c9GwWtSIH4NSJ+jMuQQPemYXi88m/QrRSGNy8//xmX8xHo2g9xGYY/4rLM+mendkEzUhg+xGuJpdSCkWsrWQICIx/ChB1W+bp3AwAAAGCOlR1O5bHw9QSEUyndoQWEU/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HLjJcivMsGEHMwQEZggIzBAQmGF5FW4YwhIv3JTKq2HndkBznIGCGeYeAAAAAAAAAAAAAAAAAAAAAAAA/fgPhc2nXMjQCzsAAAAASUVORK5CYII=”>

This we can convert into normal image, using this code.

Base64 string to Image

Code:

public Image Base64ToImage(string base64String)
{
// Convert Base64 String to byte[]

byte[] imageBytes = Convert.FromBase64String(base64String);

MemoryStream ms = new MemoryStream(imageBytes, 0,
imageBytes.Length);

// Convert byte[] to Image

ms.Write(imageBytes, 0, imageBytes.Length);

Image image = Image.FromStream(ms, true);

return image;

}

Same way we can convert

image to Base64 string

Code:

public string ImageToBase64(Image image,
System.Drawing.Imaging.ImageFormat format)
{
using (MemoryStream ms = new MemoryStream())
{

// Convert Image to byte[]

image.Save(ms, format);

byte[] imageBytes = ms.ToArray();

// Convert byte[] to Base64 String

string base64String = Convert.ToBase64String(imageBytes);

return base64String;

}

}

Thanks,
Naga Harish Movva