cejufucu
cejufucu

Reputation: 23

How can I convert IRandomAccessStreamWithContentType to BitMapImage UWP?

var contactStore = await ContactManager.RequestStoreAsync();

Contact Mycontact = await contactStore.GetContactAsync(contact.Id);

if (Mycontact.Thumbnail != null)
 {
  using (IRandomAccessStreamWithContentType stream = await Mycontact.Thumbnail.OpenReadAsync())
 {
  // todo: get bitmapimage
 }
}

I tried to use to code below to get my contact's image from UWP. My problem is: I don't know how to get a BitMap from IRandomAccessStreamWithContentType

How can I get it?

Upvotes: 1

Views: 259

Answers (1)

Jay Zuo
Jay Zuo

Reputation: 15758

When you say "BitMapImage", I'd suppose you mean Bitmap​Image Class used in UWP. If so, you can define a BitmapImage by calling SetSourceAsync and supplying a stream.

SetSourceAsync method needs a IRandomAccessStream as parameter and I​Random​Access​Stream​With​Content​Type Interface just inherits form IRandomAccessStream. So you can get a BitmapImage from Random​Access​Stream​With​Content​Type easily like the following:

if (Mycontact.Thumbnail != null)
{
    using (IRandomAccessStreamWithContentType stream = await Mycontact.Thumbnail.OpenReadAsync())
    {
        var bitmapimage = new BitmapImage();
        await bitmapimage.SetSourceAsync(stream);
        //TODO with bitmapimage
    }
}

Upvotes: 1

Related Questions