Below is the code to convert an image into base64 string in C#.
Syntax
- static void Main(string[] args)
- {
- string imagePath=@"E:\images\sample.png";
- string imgBase64String = GetBase64StringForImage(imagePath);
- Console.WriteLine(imgBase64String);
- }
- protected static string GetBase64StringForImage(string imgPath)
- {
- byte[] imageBytes = System.IO.File.ReadAllBytes(imgPath);
- string base64String = Convert.ToBase64String(imageBytes);
- return base64String;
- }
Example