我从服务器收到一个image / jpeg; base64字符串。 如何将此字符串转换为BitmapImage并设置为Image.Source?
string imgStr = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAAQABAAD .... ";
BitmapImage bmp = Base64StringToBitmap(imgStr);
myImage.Source = bmp;
提前致谢!
答案 0 :(得分:6)
我找到了解决问题的方法:
public static BitmapImage Base64StringToBitmap(string base64String)
{
byte[] byteBuffer = Convert.FromBase64String(base64String);
MemoryStream memoryStream = new MemoryStream(byteBuffer);
memoryStream.Position = 0;
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(memoryStream);
memoryStream.Close();
memoryStream = null;
byteBuffer = null;
return bitmapImage;
}