Home >Backend Development >C++ >How to Encode Images to Base64 Strings from File Paths in C#?
Encoding Images to Base64 Strings in C# from File Paths
In C#, you can convert an image selected by its path on the user's computer into a base64 string. This allows you to embed the image as a data URI within your application or send it over a network.
One way to achieve this is as follows:
Here's an example code to demonstrate:
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; } }
By applying this technique, you can transform any image on the user's computer into a base64 string, enabling you to manage and share images in a variety of scenarios.
The above is the detailed content of How to Encode Images to Base64 Strings from File Paths in C#?. For more information, please follow other related articles on the PHP Chinese website!