Get Base64 String From Image In C#

Below is the code to convert an image into base64 string in C#.

Syntax

  1. static void Main(string[] args)  
  2. {  
  3.      string imagePath=@"E:\images\sample.png";  
  4.      string imgBase64String = GetBase64StringForImage(imagePath);  
  5.      Console.WriteLine(imgBase64String);  
  6. }  
  7. protected static string GetBase64StringForImage(string imgPath)  
  8. {  
  9.       byte[] imageBytes = System.IO.File.ReadAllBytes(imgPath);  
  10.       string base64String = Convert.ToBase64String(imageBytes);  
  11.       return base64String;  
  12. }  

Example