C# 在project C Windows应用商店应用程序外部添加映像
在我的xaml文件中,有一个名为OutputImg的图像。 我还有一个名为OutputB的文本块,用于显示图像的名称,还有一个按钮,用于从图片文件夹中选择图像 代码隐藏:C# 在project C Windows应用商店应用程序外部添加映像,c#,windows-8,windows-runtime,fileopenpicker,C#,Windows 8,Windows Runtime,Fileopenpicker,在我的xaml文件中,有一个名为OutputImg的图像。 我还有一个名为OutputB的文本块,用于显示图像的名称,还有一个按钮,用于从图片文件夹中选择图像 代码隐藏: private async void Button_Click_1(object sender, RoutedEventArgs e) { FileOpenPicker openPicker = new FileOpenPicker(); openPicker.ViewMode = Picker.ViewMod
private async void Button_Click_1(object sender, RoutedEventArgs e)
{
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = Picker.ViewMode.List;
openPicker.SuggestedStartLocation = PickerLocationId.PicutresLiibrary;
openPicker.FileTypeFilter.Add(".png");
StorageFile.file = await openPicker.PickSingleFileAsync();
OutputTB.text = file.Name;
BitmapImage image = new BitmapImage(new Uri(file.path));
OutputImg.Source = image;
}
问题是,即使我没有收到任何错误,我的图片也不会显示。它会写出图片的名称以输出b.text,但图像保持为空。如何使所选图像显示在“输出图像”框中
我知道这里可能缺少一个非常基本的东西,但它只是一个学习项目您不能使用file.path为位图创建Uri对象,因为file.path提供了旧式路径,例如c:\users\…\foo.png。位图需要新样式的uri路径,例如ms-appdata:///local/path..to..file.../foo.png.
但是,据我所知,没有任何方法可以为图片库指定新样式的uri路径。因此,您必须使用一个简单的解决方法:
由于您有对文件的引用,因此可以访问文件的流,然后将该流设置为位图的源:
StorageFile file = await openPicker.PickSingleFileAsync();
OutputTB.text = file.Name;
// Open a stream for the selected file.
var fileStream =
await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
// Set the image source to the selected bitmap.
BitmapImage image = new BitmapImage();
image.SetSource(fileStream);
OutputImg.Source = image;
无法使用file.path为位图创建Uri对象,因为file.path提供了旧式路径,例如c:\users\…\foo.png。位图需要新样式的uri路径,例如ms-appdata:///local/path..to..file.../foo.png.
但是,据我所知,没有任何方法可以为图片库指定新样式的uri路径。因此,您必须使用一个简单的解决方法:
由于您有对文件的引用,因此可以访问文件的流,然后将该流设置为位图的源:
StorageFile file = await openPicker.PickSingleFileAsync();
OutputTB.text = file.Name;
// Open a stream for the selected file.
var fileStream =
await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
// Set the image source to the selected bitmap.
BitmapImage image = new BitmapImage();
image.SetSource(fileStream);
OutputImg.Source = image;