How to load an image from a base64 encoded png in UWP app C++/CX

KGabesz 1 Reputation point


I have a base64 encoded PNG file (a QR code), and I would like to show the picture on a Page.

I have this XAML element:

<Image Grid.Row="3" Name="QRCode" Height="200" Width="200"/>  

I converted the base64 string to an IBuffer:

Windows::Storage::Streams::IBuffer^ buf = Windows::Security::Cryptography::CryptographicBuffer::DecodeFromBase64String(base64string);  

Now I would like to create a BitmapImage, to set it as the source of the Image on the page:

BitmapImage^ Image = ref new BitmapImage();  
QRCode->Source = Image;  

The question is, that how can I set the IBuffer as the source of the Image? What is the missing step?

Thanks if you help me!

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 71,621 Reputation points

    You can use the GetPointerToPixelData function from Obtaining pointers to data buffers (C++/CX)
    with a WriteableBitmap that you can set as source of an Image

    0 comments No comments

  2. KGabesz 1 Reputation point

    Meanwhile I found a working solution, so I paste it here, to help others who are facing with the same problem:

    Windows::Storage::Streams::IBuffer^ buf = Windows::Security::Cryptography::CryptographicBuffer::DecodeFromBase64String(base64string);  
    auto bitmapImage = ref new BitmapImage();  
    QRCode->Source = bitmapImage;  
    auto stream = ref new InMemoryRandomAccessStream();  
    auto writer = ref new DataWriter(stream->GetOutputStreamAt(0));  
    create_task(writer->StoreAsync()).then([=](unsigned bytesStored)  
    return bitmapImage->SetSourceAsync(stream);  
    0 comments No comments