在本地网络游戏中与WinRT中的其他人共享本地用户的个人资料图片

时间:2013-07-05 12:56:25

标签: c# windows windows-runtime

我正在开发一款WinRT应用,这是一款本地网络可玩游戏。我可以在应用程序中读取用户的DisplayName和AccountPicture,并通过xaml在本地向用户显示它们。我使用以下代码将帐户图片转换为BitmapImage以便在xaml页面中进行绑定:

private async Task<BitmapImage> GetDisplayImage()
    {
        BitmapImage image = null;

        if (CoreApplication.MainView != null)
        {
            await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
                CoreDispatcherPriority.Normal, async () =>
                {
                    var stream = await Windows.System.UserProfile.UserInformation.GetAccountPicture(Windows.System.UserProfile.AccountPictureKind.LargeImage).OpenReadAsync();
                    image = new BitmapImage();
                    image.SetSource(stream);
                });
        }
        return image;
    }

我认为可以将流读入字节数组,并在应用程序连接到其他数据库时发送它,然后在另一端重新构建字节数组,如下所示:

public static async Task<byte[]> ToArray(this IRandomAccessStream stream)
    {
        IBuffer buffer = new Windows.Storage.Streams.Buffer((uint)stream.Size);
        await stream.ReadAsync(buffer, buffer.Capacity, InputStreamOptions.ReadAhead);
        await stream.FlushAsync();
        return buffer.ToArray();
    }

然而,在线:

await stream.FlushAsync();

我收到了UnauthorizedAccessException:

Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

必须可以与其他人共享个人资料图片,因为我看到它在其他地方完成,但我似乎无法找到有关如何执行此操作的任何信息。如果有人能指出我正确的方向(我想知道是否有一个不同的api我需要挂钩,或者如果我需要在App清单中设置一些东西......),我会非常感激。

1 个答案:

答案 0 :(得分:0)

您可以直接阅读IStorageFile返回的GetAccountPicture

var file = UserInformation.GetAccountPicture(AccountPictureKind.LargeImage);
var buffer = await FileIO.ReadBufferAsync(file);
var bytes = buffer.ToArray();