WPF 基础

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)); 
原文地址:https://www.cnblogs.com/MichaelLoveSna/p/14497005.html