C# 图片转 Base64 编码并写入 TXT 文件

以下是一个简单的示例,可以将图片转换为 Base64 编码,然后将其写入文本文件中。

using System;
using System.IO;

namespace ImageToBase64
{
    class Program
    {
        static void Main(string[] args)
        {
            // 读取图片文件
            string imagePath = 'C:\path\to\image.jpg';
            byte[] imageBytes = File.ReadAllBytes(imagePath);

            // 将图片转换为 Base64 编码
            string base64String = Convert.ToBase64String(imageBytes);

            // 写入 Base64 编码到文本文件
            string txtPath = 'C:\path\to\output.txt';
            File.WriteAllText(txtPath, base64String);

            Console.WriteLine('图片已转换为 Base64 编码并写入文件中。');
        }
    }
}

请注意:

  • 这只是一个简单的示例,可能需要处理一些异常情况,例如文件不存在或无法读取等。
  • 如果要将多个图片文件转换为 Base64 编码并写入同一个文本文件中,则需要对代码进行修改。
C# 图片转 Base64 编码并写入 TXT 文件

原文地址: https://www.cveoy.top/t/topic/ofIR 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录