WPF 基础 - 图片与 base64

1. base64 转图片

  1. 将 base64 转成 byte[]
  2. 将 byte[] 作为内存流保存到一个 BitmapImage 实例的流的源
  3. 把 BitmapImage 作为目标图片的 Source
byte[] streamBase = Convert.FromBase64String(imagebase64);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = new MemoryStream(streamBase);
bi.EndInit();
img.Source = bi;

如果 StreamSource 和 UriSource 均设置,则忽略 StreamSource 值。

2. 图片转 base64

  1. 读取图片的内容读入一个字节数组 byte[]
  2. 将字节数组转为 base64
Convert.ToBase64String(File.ReadAllBytes(imageFilepath)); 
posted @ 2021-03-08 21:46  鑫茂  阅读(504)  评论(0编辑  收藏  举报