el-niko
el-niko

Reputation: 113

Convert Base64 string to BitmapImage C# Windows Phone

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!

Upvotes: 3

Views: 8923

Answers (1)

el-niko
el-niko

Reputation: 113

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

Upvotes: 6

Related Questions