Convert image path to base64 string

How to convert image to base64 string in C #?

For example, I have a path to an image C:/image/1.gif and would like to return data:image/gif;base64,/9j/4AAQSkZJRgABAgEAYABgAAD..

+47
c # image base64
Jan 24 '14 at 6:11
source share
6 answers

try it

 using (Image image = Image.FromFile(Path)) { using (MemoryStream m = new MemoryStream()) { image.Save(m, image.RawFormat); byte[] imageBytes = m.ToArray(); // Convert byte[] to Base64 String string base64String = Convert.ToBase64String(imageBytes); return base64String; } } 
+95
Jan 24 '14 at 6:18
source share

Get a representation of the byte array ( byte[] ) of the image, then use Convert.ToBase64String() , st. eg:

 byte[] imageArray = System.IO.File.ReadAllBytes(@"image file path"); string base64ImageRepresentation = Convert.ToBase64String(imageArray); 

To convert the base4 image back to System.Drawing.Image:

 var img = Image.FromStream(new MemoryStream(Convert.FromBase64String(base64String))); 
+41
Jan 24 '14 at 6:15
source share

Since most of us, as oneliners:

 Convert.ToBase64String(File.ReadAllBytes(imageFilepath)); 

If you need it as an array of Base64 bytes:

 Encoding.ASCII.GetBytes(Convert.ToBase64String(File.ReadAllBytes(imageFilepath))); 
+5
Jul 18 '17 at 14:56
source share

This is the class I wrote for this purpose:

 public class Base64Image { public static Base64Image Parse(string base64Content) { if (string.IsNullOrEmpty(base64Content)) { throw new ArgumentNullException(nameof(base64Content)); } int indexOfSemiColon = base64Content.IndexOf(";", StringComparison.OrdinalIgnoreCase); string dataLabel = base64Content.Substring(0, indexOfSemiColon); string contentType = dataLabel.Split(':').Last(); var startIndex = base64Content.IndexOf("base64,", StringComparison.OrdinalIgnoreCase) + 7; var fileContents = base64Content.Substring(startIndex); var bytes = Convert.FromBase64String(fileContents); return new Base64Image { ContentType = contentType, FileContents = bytes }; } public string ContentType { get; set; } public byte[] FileContents { get; set; } public override string ToString() { return $"data:{ContentType};base64,{Convert.ToBase64String(FileContents)}"; } } var base64Img = new Base64Image { FileContents = File.ReadAllBytes("Path to image"), ContentType="image/png" }; string base64EncodedImg = base64Img.ToString(); 
+3
Jul 21 '16 at 19:59
source share

You can use the Server.Map path to provide a relative path, and then you can either create the image using base64 conversion, or just add the base64 string to image src .

 byte[] imageArray = System.IO.File.ReadAllBytes(Server.MapPath("~/Images/Upload_Image.png")); string base64ImageRepresentation = Convert.ToBase64String(imageArray); 
0
Jan 25 '17 at 20:40
source share

You can convert it like this:

  string test = @"C:/image/1.gif"; byte[] bytes = System.Text.ASCIIEncoding.ASCII.GetBytes(test); string base64String = System.Convert.ToBase64String(bytes); Console.WriteLine("Base 64 string: " + base64String); 

Exit

  QzovaW1hZ2UvMS5naWY= 
-one
Jan 24 '14 at 6:16
source share



All Articles