木子屋 Dnawo's BLOG

C#将图片音频等文件转成Base64字符串示例

👤 dnawo 📅 2020-10-31 👁 4031 👍 0 💬 0 🔄 本站原创
class Program
{
    static void Main(string[] args)
    {
        Console.Write("文件地址:");
        string file = Console.ReadLine();
        Console.WriteLine("转换结果:{0}", FileToBase64String(file));
        Console.ReadLine();
    }

    static string FileToBase64String(string file)
    {
        string result = string.Empty;

        try
        {
            using (FileStream stream = new FileStream(file, FileMode.Open))
            {
                byte[] bt = new byte[stream.Length];
                stream.Read(bt, 0, bt.Length);
                result = Convert.ToBase64String(bt);
            }
        }
        catch { }

        return result;
    }
}

HTML5使用示例

图片:
<img src="data:image/png;base64,转换的Base64字符串"/>

音频:
<audio src="data:audio/wav;base64,转换的Base64字符串 />

评论(0)

暂无评论。

计算题
评论需审核通过后显示
← 上一篇 长安星卡S201没开空调驾驶室漏水故障 下一篇 → emoji表情符号大全(非图片、可直接复制)