xamarin android generate image

cristopheB 551 Reputation points
2021-06-15T07:29:41.28+00:00

Hello,

we have an xamarin android app who call an API who send a QRCode as a string
Look the print screen i join, here is it what i receive after the call to the api ..

Now i would like to transform this string into a image but i can't ..

Someone has idea how to transform string into image ?!
105578-qrcodequestion.png

thanks for all

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Answer accepted by question author
  1. Harrison of The North 196 Reputation points
    2021-06-15T12:06:37.973+00:00

    Hi @cristopheB

    I'm assuming that your API has returned the image, and then converted the image to Base64 (a common way of passing images around as text). If this assumption is wrong, please clarify your case!

    If my assumption is correct, then having received the image as text in a Base64 string, you can then decode it using:

       var byteArray = Convert.FromBase64String(base64text);  
    

    then what you have is the same byte array as if you had opened the image file from disk and converted it to a byte array.

    So in this case you just need to convert it to a Stream:

       Stream stream = new MemoryStream(byteArray);  
    

    At this point, just pass the Stream to your image, I don't have the Xamarin.Android code at hand, but to illustrate the process using the Xamarin.Forms equivalent:

       var byteArray = Convert.FromBase64String(base64text);  
       Stream stream = new MemoryStream(byteArray);  
       var imageSource = ImageSource.FromStream(()=>stream);  
       MyImage.Source = imageSource;  
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.