【问题标题】:How can I convert IRandomAccessStreamWithContentType to BitMapImage UWP?如何将 IRandomAccessStreamWithContentType 转换为 BitMapImage UWP?
【发布时间】:2017-05-29 15:51:23
【问题描述】:
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

获取位图

我怎样才能得到它?

【问题讨论】:

标签: c# uwp


【解决方案1】:

当您说“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
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-19
    • 1970-01-01
    • 2016-07-06
    • 2012-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多