/// <summary>
/// Baser64Code 的摘要说明。
/// </summary>
public class Baser64Code
{
/// <summary>
/// 编码的静态方法
/// </summary>
/// <param name="filepath"></param>
/// <returns>编码后的字符串</returns>
public static string ConvertBase64(string filepath)
{
//变量
string result = string.Empty;
//将文件转换为stream
using(FileStream fs = new FileStream(filepath,FileMode.Open))
{
byte[] buffer = new byte[fs.Length];
fs.Read(buffer,0,buffer.Length);
result = Convert.ToBase64String(buffer); //base64编码
}
//返回编码后的字符串
return result;
}
/// <summary>
/// 解码的静态方法
/// </summary>
/// <param name="strBase64"></param>
/// <returns>保存路径</returns>
public static string FromBase64(string strBase64)
{
//存放图片的路径
string imgPath = @"F:\1.gif";
//将base64编码存入byte字节
byte[] buffer = Convert.FromBase64String(strBase64);
FileStream fs = new FileStream(imgPath,FileMode.Create);
//将字节写入图片
fs.Write(buffer,0,buffer.Length);
//返回路径
return imgPath;
}