我从服务器收到一个 image/jpeg;base64 字符串。
如何将此字符串转换为 BitmapImage 并设置为 Image.Source ?

string imgStr = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAAQABAAD .... ";
BitmapImage bmp = Base64StringToBitmap(imgStr);
myImage.Source = bmp;

提前致谢!

最佳答案

我为我的问题找到了解决方案:

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;
}

关于c# - 将 Base64 字符串转换为 BitmapImage C# Windows Phone,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23624865/

10-12 14:21