【问题标题】:UWP - ImageBrush full path to ImageSourceUWP - ImageBrush ImageSource 的完整路径
【发布时间】:2016-04-20 07:28:49
【问题描述】:

我有一个椭圆,我想用图像填充它。我想将图像的完整路径设置为ImageBrushImageSource 属性。但我无法完成它。

我已经尝试添加:

  • C:/Users/someuser/Pictures/untitled.png

  • C:\\Users\\someuser\\Pictures\\untitled.png 来自FilePicker

  • C:\Users\misko\Pictures\untitled.png.

但这些都不起作用。你能解释一下如何正确设置完整路径吗?

【问题讨论】:

    标签: win-universal-app


    【解决方案1】:

    你必须先用FilePicker打开文件

    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
        };
    }
    

    【讨论】:

    • 所以只能将位图绑定到ImageBrush的ImageSource上?
    • 是的,没有办法使用应用程序沙箱之外的文件的完整路径来绑定到 ImageSource
    • 除了默认位置之外,UWP 应用可以访问某些特殊文件夹中的文件,前提是在应用的功能中声明了文件访问权限。详情请见this article
    • 没错,您可以访问该文件。这就是为什么我可以读取它来创建位图图像。
    • 所以如果我想记住用户选择的图像,我不能简单地记住图像的路径,而是必须将其复制并存储在应用程序本地文件夹中?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-02
    • 2014-05-01
    • 2017-03-16
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    相关资源
    最近更新 更多