将图像(按路径选择)转换为base64字符串 [英] Convert an image (selected by path) to base64 string
本文介绍了将图像(按路径选择)转换为base64字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将图像从用户计算机上的路径转换为C#中的base64字符串?
How do you convert an image from a path on the user's computer to a base64 string in C#?
例如,我具有图像的路径(格式为C:/image/1.gif
),并且希望具有像data:image/gif;base64,/9j/4AAQSkZJRgABAgEAYABgAAD..
这样的数据URI,表示返回的1.gif
图像.
For example, I have the path to the image (in the format C:/image/1.gif
) and would like to have a data URI like data:image/gif;base64,/9j/4AAQSkZJRgABAgEAYABgAAD..
representing the 1.gif
image returned.
推荐答案
尝试一下
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;
}
}
这篇关于将图像(按路径选择)转换为base64字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文