gpt4 book ai didi

c# - 在项目 C# Windows 应用商店应用程序之外添加图像

转载 作者:行者123 更新时间:2023-11-30 22:19:22 25 4
gpt4 key购买 nike

在我的 xaml 文件中,我有一个名为 OutputImg 的图像。我还有一个名为 OutputTB 的文本 block ,用于显示图像的名称,还有一个按钮,可让我从我的图片文件夹中选择图像。

代码隐藏:

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

问题是即使我没有收到任何错误,我的图片也不会显示。它将图片的名称写出到 OutputTB.text 但 Image 只是保持为空。如何让我选择的图像出现在 OutputImg 图像框中。

我知道我可能在这里遗漏了一个非常基本的东西,但这只是一个学习项目

最佳答案

您不能使用 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;

关于c# - 在项目 C# Windows 应用商店应用程序之外添加图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15623656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com