如何在C#中将用户计算机上的图像路径转换为base64字符串?
例如,我有图像路径(格式如C:/image/1.gif
),并希望获得类似于data:image/gif;base64,/9j/4AAQSkZJRgABAgEAYABgAAD..
的数据URI,以表示返回的1.gif
图像。
string image_path="physical path of your image";
byte[] byes_array = System.IO.File.ReadAllBytes(Server.MapPath(image_path));
string base64String = Convert.ToBase64String(byes_array);
类似这样的东西
Function imgTo64(ByVal thePath As String) As String
Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(thePath)
Dim m As IO.MemoryStream = New IO.MemoryStream()
img.Save(m, img.RawFormat)
Dim imageBytes As Byte() = m.ToArray
img.Dispose()
Dim str64 = Convert.ToBase64String(imageBytes)
Return str64
End Function
C#
标签吗? - ADyson