gpt4 book ai didi

c# - 如何从 UWP 格式的文件中加载图像

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

我以前是一名 IT 专业人员,使用各种软件进行了大量的数据库编程,摆弄各种编程语言,从 PL/1 和 Pascal 到最新的 Python。我正在尝试在 UWP 环境中学习 c#,以期编写一些混合现实应用程序。是的,我知道我雄心勃勃……因为现在它根本不起作用!哈哈!

我给自己做了一个“简单”的小项目,开始摆弄类(class)并学习软件。这是一个使用 .Net 框架的 c# 中的简单幻灯片软件,因为我希望能够在混合现实和 2D 应用程序中使用它。我已经用 Python 对此进行了编程,所以我正在尝试将其移植到 c#(我知道它不是直接移植...我的天...)。天哪,我在谷歌搜索中找不到任何像样的教程或代码片段。很多在没有 .Net 的情况下在 c# 中,很多在其他语言中,但不是在我尝试使用的环境中。

所以!我已经在 Visual Studio 2017 中使用空白 UWP 应用程序创建了我的应用程序。我正在使用工具箱中的“图像”并将其放在我的屏幕上,创建了一个 AppBarButton 来放置我的控件。我已经创建(使用其他示例)一个选择器,它返回一个存储文件以选择一个 jpg 文件,并且能够将名称放在我在同一屏幕上创建的文本框中。

但是!!!

经过几个小时的摆弄,我找不到如何在我用 XAML 创建的“图像”中加载图像。此外,我还想操纵这张图片、缩放它、旋转它,但我不知道该往哪里看。最后,如果有人可以指导我如何从目录中读取整个文件列表以及如何使用 Zip 文件,我们将不胜感激。

如果你有代码片段,我会通读一遍,或者如果你有我应该做的教程,请提出建议!!!但是,作为一名经验丰富的 IT 人员,我正在寻找中肯的说明。我曾尝试查看 Microsoft C# 教程,但对那些俗气的笑话和缓慢的节奏失去了耐心……我在生活中是一个非常有耐心的人……

为了便于引用,我在此处包含了我现在所处位置的 XAML 和 C# 代码。经过这么多年的编程,感觉自己像个 child 一样学会说话,真是令人沮丧!哈哈!

XAML 部分:

<Page.BottomAppBar>
<CommandBar>
<CommandBar.Content>
<Grid/>
</CommandBar.Content>
<AppBarButton Icon="OpenFile" Label="AppBarButton" Tapped="Loadmedia"/>
<AppBarButton Icon="Next" Label="AppBarButton"/>
</CommandBar>
</Page.BottomAppBar>

<Grid>
<TextBox x:Name="outtext" HorizontalAlignment="Left" VerticalAlignment="Bottom" Height="73" Width="1343" Margin="315,0,0,10"/>
<Image x:Name="mypic" HorizontalAlignment="Left" Height="765" Margin="48,45,0,0" VerticalAlignment="Top" Width="1783" FocusVisualPrimaryBrush="Black"/>
</Grid>
</Page>

这是 C# 代码:

    public MainPage()
{
this.InitializeComponent();
}

private async void Loadmedia(object sender, TappedRoutedEventArgs e)
{
var picker = new FileOpenPicker();
Image img = sender as Image;

picker.ViewMode = PickerViewMode.Thumbnail;

picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;

picker.FileTypeFilter.Add(".jpg");

StorageFile file = await picker.PickSingleFileAsync();

outtext.Text = file.Path;

mypic.UriSource = new BitmapImage(new Uri(file.Path));



}

}

最佳答案

使用 SetSourceAsync 和从 StorageFile 打开的流设置 BitmapImage 的源,而不是仅使用 StorageFile 的路径。应用程序没有权限直接访问路径,需要通过 StorageFile 通过文件代理。

BitmapSource.SetSourceAsync documentation 中有一个示例代码片段一旦修改为您的变量名,它看起来基本上像这样:

// Ensure the stream is disposed once the image is loaded
using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
// Set the image source to the selected bitmap
BitmapImage bitmapImage = new BitmapImage();
// Decode pixel sizes are optional
// It's generally a good optimisation to decode to match the size you'll display
bitmapImage.DecodePixelHeight = decodePixelHeight;
bitmapImage.DecodePixelWidth = decodePixelWidth;

await bitmapImage.SetSourceAsync(fileStream);
mypic.Source = bitmapImage;
}

查看 Simple Imaging sampleBasic input sample例如旋转、缩放等。

关于c# - 如何从 UWP 格式的文件中加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52104377/

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