【问题标题】:Convert Base64 string to BitmapImage C# Windows Phone将 Base64 字符串转换为 BitmapImage C# Windows Phone
【发布时间】:2015-02-12 21:06:16
【问题描述】:

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

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

提前致谢!

【问题讨论】:

  • 您可以使用byte[] byteBuffer = Convert.FromBase64String(base64String); 获取字节,但不知道如何在图像控件中设置。
  • 维托卡诺瓦,谢谢!我可以将 byteBuffer 与 memoryStream 一起使用,并将其设置为 BitmapImage 的 Source!
  • 也许发布您的解决方案作为答案。

标签: c# windows-phone base64 jpeg bitmapimage


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    • 2014-12-07
    相关资源
    最近更新 更多