2017-05-27 54 views
0
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 
} 
} 

我试着用下面的代码从UWP中获取我的联系人图片。我的问题是:我不知道如何从IRandomAccessStreamWithContentType得到一个位图如何将IRandomAccessStreamWithContentType转换为BitMapImage UWP?

我怎样才能得到它呢?

+0

请参考[这里](https://stackoverflow.com/questions/15328084/convert-irandomaccessstreamwithcontenttype-to-byte)的答案。 – CodingYoshi

+0

[将IRandomAccessStreamWithContentType转换为字节\ [\]]的可能重复(https://stackoverflow.com/questions/15328084/convert-irandomaccessstreamwithcontenttype-to-byte) – Razor

回答

0

当你说“BitMapImage”时,我假设你的意思是在UWP中使用Bitmap​Image Class。如果是这样,您可以通过调用SetSourceAsync并提供一个流来定义BitmapImage

SetSourceAsync方法需要IRandomAccessStream作为参数和I​Random​Access​Stream​With​Content​Type接口只是继承形式IRandomAccessStream。所以,你可以从一个Random​Access​Stream​With​Content​Type容易BitmapImage类似如下:

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