如需将图片转为base64编码,请先将图片转为byte[] 字节数组,转换方法可参考 C# 图片和字节数组byte[]互转
/// <summary> /// 将字节数组为Base64编码 /// </summary> /// <param name="bytebase">要编码的byte[]</param> /// <returns></returns> public static string ByteToBase64(byte[] bytebase) { return Convert.ToBase64String(bytebase); } /// <summary> /// 将Base64字符串转为byte[] /// </summary> /// <param name="str">Base64字符串</param> /// <returns></returns> public static byte[] Base64ToByte(string str) { return Convert.FromBase64String(str); } /// <summary> /// 将字符串转为Base64编码 /// </summary> /// <param name="str">要编码的string字符</param> /// <param name="encoding">字符编码方案</param> /// <returns></returns> public static string StringToBase64(string str, Encoding encoding) { byte[] buff = encoding.GetBytes(str); return Convert.ToBase64String(buff); } /// <summary> /// 将Base64编码解析成字符串 /// </summary> /// <param name="strbase">要解码的string字符</param> /// <param name="encoding">字符编码方案</param> /// <returns></returns> public static string Base64ToString(string strbase, Encoding encoding) { byte[] buff = Convert.FromBase64String(strbase); return encoding.GetString(buff); }
评论前必须登录!
注册