Convert Base64 string to BitmapImage C# Windows Phone

拥有回忆 提交于 2019-12-01 23:37:18

问题


I receive a image/jpeg;base64 string from server. How can I convert this string to BitmapImage and set like Image.Source ?

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

Thanks in advance!


回答1:


I find solution for my issue:

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


来源:https://stackoverflow.com/questions/23624865/convert-base64-string-to-bitmapimage-c-sharp-windows-phone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!