Encode an Image to Base64 Using C#
Many applications require the ability to embed images within other formats, such as JSON or XML. In C#, converting an image to a base64 string allows for easy storage and transmission of image data.
To convert an image to a base64 string, follow these steps:
Here is an example implementation to convert an image located at "C:/image/1.gif" to a base64 string:
using (Image image = Image.FromFile("C:/image/1.gif")) { 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; } }
The above is the detailed content of How to Encode an Image to a Base64 String in C#?. For more information, please follow other related articles on the PHP Chinese website!