2013-08-24 82 views
2

我想将图像源设置为从我的电脑(不在资产中)。
这是我正在试图做到这一点:设置图像源

Uri uri = new Uri(@"D:\Riot Games\about.png", UriKind.Absolute); 
ImageSource imgSource = new BitmapImage(uri); 

this.image1.Source = imgSource; 

我几乎尝试了所有我能找到在互联网上,但似乎没有任何工作。

任何想法为什么?

XAML:

<UserControl 
    x:Class="App11.VideoPreview" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:App11" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    d:DesignHeight="250" 
    d:DesignWidth="250"> 

    <Grid> 
     <Button Height="250" Width="250" Padding="0" BorderThickness="0"> 
      <StackPanel> 
       <Image Name="image1" Height="250" Width="250"/> 
       <Grid Margin="0,-74,0,0"> 
        <Grid.Background> 
         <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0" Opacity="0.75"> 
          <GradientStop Color="Black"/> 
          <GradientStop Color="#FF5B5B5B" Offset="1"/> 
         </LinearGradientBrush> 
        </Grid.Background> 
        <TextBlock x:Name="textBox1" TextWrapping="Wrap" Text="test" FlowDirection="RightToLeft" Foreground="White" Padding="5"/> 
       </Grid> 
      </StackPanel> 
     </Button> 
    </Grid> 
</UserControl> 
+0

它必须工作。你能显示使用.xaml吗? –

+0

添加.xaml代码 – Ron

+2

可能是因为用户权限?尝试运行.exe作为administartor。 –

回答

7

你不能从你的Windows应用程序地铁直接访问磁盘驱动器。从File access permissions in windows store apps

提取您可以在默认情况下访问某些文件系统位置,如应用程序安装目录 ,应用程序数据的位置,以及下载文件夹,在Windows 商店的应用程序。应用程序还可以通过文件选取器或声明功能访问其他位置 。

但是还有一些特殊的文件夹,你可以像Pictures library那样访问文件库等等,方法是从包清单文件启用功能。因此,该代码会从清单文件中启用图片库后才能正常运行(复制about.png在图片文件库文件夹)

private async void SetImageSource() 
    { 
     var file = await 
      Windows.Storage.KnownFolders.PicturesLibrary.GetFileAsync("about.png"); 
     var stream = await file.OpenReadAsync(); 
     var bitmapImage = new BitmapImage(); 
     bitmapImage.SetSource(stream); 

     image1.Source = bitmapImage; 
    } 

但理想的解决方案将包括您在您的应用程序文件,并将其生成操作内容以便它可以与其他内容文件一起复制到您的Appx文件夹中。然后,你可以这样设置图像源 -

public MainPage() 
    { 
     this.InitializeComponent(); 
     Uri uri = new Uri(BaseUri, "about.png"); 
     BitmapImage imgSource = new BitmapImage(uri); 
     this.image1.Source = imgSource; 
    } 

或者你可以简单地仅仅做到这一点在XAML:

<Image x:Name="image1" Source="ms-appx:/about.png"/> 

这里是特殊的文件夹列表,您可以从您的应用程序访问 -

  1. 本地应用程序数据
  2. 漫游应用程序数据
  3. 临时应用程序数据
  4. 应用程序安装位置
  5. Downloads文件夹
  6. 文档库
  7. 音乐库
  8. 图片库
  9. 视频库
  10. 移动设备
  11. 家庭组
  12. 媒体服务器设备

为了能够从您的清单文件功能,双击在Package.appxmanifest文件中解决方案并在功能选项卡下勾选Pictures Library复选框,以便为您的应用程序启用它。同样,您可以为您要访问的其他文件夹执行此操作。

enter image description here

+0

我从整个计算机上获取图像,我不能将它们放在特定位置或将它们嵌入到我的资产中。 – Ron

+0

恐怕这对于地铁应用程序的限制是不可能的。 –

+0

我设法使用StorageFile设置图像源。但现在还有另一个问题。我如何设置StorageFile使用直接uri而不是filepicker? – Ron