如何从People App获取个人资料图片(图片)

时间:2013-12-24 11:21:36

标签: windows-8 windows-runtime microsoft-metro windows-store-apps winrt-xaml

我想从People App获取个人资料图片。我用了

Windows.ApplicationModel.Contacts.Contact contact = new Contact();

我从倾斜联系中得到了缩略图.Thumbnail。

我需要将此缩略图转换为StorageFile。您能否提供一些意见来解决这个问题?

并且,在使用以下代码时:

IRandomAccessStreamWithContentType stream = awaitcontactInfo.Thumbnail.OpenReadAsync();

if(stream != null && stream.Size > 0)
{
  //
}

有时候我会RPC Server is unavailable Exception。有时streamSize为零。

1 个答案:

答案 0 :(得分:0)

您正在创建Contact类的新实例。您不必这样做以从人们那里选择联系人。您应该使用ContactPicker

var contactPicker = new Windows.ApplicationModel.Contacts.ContactPicker();
contactPicker.CommitButtonText = "Select";
ContactInformation contact = await contactPicker.PickSingleContactAsync();

if (contact != null)
{
    IRandomAccessStreamWithContentType stream = await contact.GetThumbnailAsync();
    if (stream != null && stream.Size > 0)
    {
        var file = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("MyContactThumb.png", CreationCollisionOption.GenerateUniqueName);
        // You can also use FileSavePicker to save file in user defined location.
        Windows.Storage.Streams.Buffer MyBuffer = new Windows.Storage.Streams.Buffer(Convert.ToUInt32(stream.Size));
        IBuffer iBuf = await stream.ReadAsync(MyBuffer, MyBuffer.Capacity, InputStreamOptions.None);
        await FileIO.WriteBufferAsync(file, iBuf);
    }
}