wpf 设置图片来源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18417897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set image source
提问by Ron
I am trying to set image source to something from my computer (not in the assets).
This is how I am trying to do this:
我正在尝试将图像源设置为我计算机中的某些内容(不在资产中)。
这就是我试图做到这一点的方式:
Uri uri = new Uri(@"D:\Riot Games\about.png", UriKind.Absolute);
ImageSource imgSource = new BitmapImage(uri);
this.image1.Source = imgSource;
I tried almost everything I could find in the internet but nothing seem to work.
我尝试了几乎所有可以在互联网上找到的东西,但似乎没有任何效果。
Any idea why?
知道为什么吗?
XAML:
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>
回答by Rohit Vats
You cannot access disk drives directly from your windows metro apps. Extracted from File access permissions in windows store apps
您无法直接从 Windows Metro 应用程序访问磁盘驱动器。从Windows Store 应用程序中的文件访问权限中提取
You can access certain file system locations, like the app install directory, app data locations, and the Downloads folder, with Windows Store apps by default. Apps can also access additional locations through the file picker, or by declaring capabilities.
默认情况下,您可以使用 Windows 应用商店应用访问某些文件系统位置,例如应用安装目录、应用数据位置和下载文件夹。应用程序还可以通过文件选择器或通过声明功能来访问其他位置。
But there are some special folders which you can access like Pictures library, documents library etc. by enabling capabilities from your package manifest file. So, this code will work after enabling pictures library from manifest file (copy about.png file in pictures library folder)
但是,您可以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;
}
But ideal solution would be to include you file in you application and set its build action to Content so that it can be copied in your Appxfolder along with other content files. Then you can set the image source like this -
但理想的解决方案是将您的文件包含在您的应用程序中,并将其构建操作设置为 Content,以便它可以与其他内容文件一起复制到您的Appx文件夹中。然后你可以像这样设置图像源 -
public MainPage()
{
this.InitializeComponent();
Uri uri = new Uri(BaseUri, "about.png");
BitmapImage imgSource = new BitmapImage(uri);
this.image1.Source = imgSource;
}
OR you can simply do this in XAML only :
或者您可以仅在 XAML 中简单地执行此操作:
<Image x:Name="image1" Source="ms-appx:/about.png"/>
Here is list of special folders which you can access from your application -
以下是您可以从应用程序访问的特殊文件夹列表 -
- Local App data
- Roaming app data
- Temporary app data
- App installed location
- Downloads folder
- Documents library
- Music library
- Pictures library
- Videos library
- Removable devices
- Home group
- Media Server devices
- 本地应用数据
- 漫游应用数据
- 临时应用数据
- 应用安装位置
- 下载文件夹
- 文档库
- 曲库
- 图片库
- 影片库
- 移动设备
- 家庭组
- 媒体服务器设备
To enable capabilities from your manifest file, double click on Package.appxmanifestfile in your solution and check Pictures Librarycheckbox under capabilities tab to enable it for your application. Likewise you can do it for other folders which you want to access.
要从清单文件启用功能,请双击Package.appxmanifest解决方案中的文件并Pictures Library选中功能选项卡下的复选框,为您的应用程序启用它。同样,您可以对要访问的其他文件夹执行此操作。



