对文本的编码与解码
static void Main(string[] args)
{
string res = base64WithStr("阳光梦想:https://www.yangguangdream.com");
string res2 = strWithBase64(res);
}
//编码
public static string base64WithStr(string sourceStr)
{
byte[] bytes = Encoding.Default.GetBytes(sourceStr);
string res = Convert.ToBase64String(bytes);
return res;
}
//解码
public static string strWithBase64(string base64)
{
byte[] bytes = Convert.FromBase64String(base64);
string res = Encoding.Default.GetString(bytes);
return res;
}对图片的编码与解码
static void Main(string[] args)
{
string res=base64WithImage(@"D:\qrcodeapi.png");
Bitmap bmp = imageWithBase64(res);
bmp.Save(@"D:\base64result.png");
}
//编码
public static string base64WithImage(string path)
{
MemoryStream stream = new MemoryStream();
Bitmap bmp = new Bitmap(path);
bmp.Save(stream,ImageFormat.Jpeg);
byte[] bytes = stream.GetBuffer();
string res = Convert.ToBase64String(bytes);
return res;
}
//解码
public static Bitmap imageWithBase64(string base64)
{
byte[] bytes = Convert.FromBase64String(base64);
MemoryStream stream = new MemoryStream(bytes);
Bitmap bmp = new Bitmap(stream);
return bmp;
}
微信扫码查看本文
发表评论