将ImageSource设置为应用程序包外部的路径

时间:2016-04-17 18:47:37

标签: c# image win-universal-app imagesource

如何更改应用程序包之外的ImageSource?我用这个:

<Border BorderBrush="Black" BorderThickness="1" Margin="315,260,85,140" Width="100" Height="100">
    <Grid x:Name="grid" Margin="0" Visibility="Visible">
    </Grid>
</Border>

但是,在我选择了我想要的图像之后,它并没有显示为网格背景。它没有显示!这是XAML:

{{1}}

谢谢!

1 个答案:

答案 0 :(得分:0)

您必须先打开文件:

if (file != null)
{
    var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

    var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
    await bitmapImage.SetSourceAsync(stream);

    var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);

    grid.Background = new ImageBrush
    {

        ImageSource = bitmapImage
    };
}