2017-05-27 81 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 
    } 
} 
相關問題