如何在IRandomAccessStreamReference中为Shape.Fill使用图像

时间:2016-03-20 23:03:57

标签: c# win-universal-app

我有一个存储为IRandomAccessStreamReference的图像,我需要将其用作Ellipse中的填充。我该如何使用它?

...
 static IRandomAccessStreamReference myThumbnail;
...
 var contactStore = await ContactManager.RequestStoreAsync();
 var contact = await contactStore.GetMeContactAsync();
 myThumbnail = contact.Thumbnail;
...
 Ellipse ellipse = new Ellipse
            {

                Height = 32,
                Width = 32,
                Fill = myThumbnail, // How do I use this?
             };

1 个答案:

答案 0 :(得分:0)

因此,您需要使用ImageBrush类,该类派生自基类Brush类。 该类包含ImageSource属性,BitmapImage的基类。另外,例如BitmapImage类,我建议使用DecodePixelWidthDecodePixelHeight属性。

var thumbailImageBrush = new ImageBrush();
using (var randomStream = await myThumbnail.OpenReadAsync())
{
    var bitmapImage = new BitmapImage();
    bitmapImage.DecodePixelWidth = 32; // just good practice
    await bitmapImage.SetSourceAsync(randomStream);
    thumbailImageBrush.ImageSource = bitmapImage;
}

var ellipse = new Ellipse
{
    Height = 32,
    Width = 32,
    Fill = thumbailImageBrush
};