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

How to load an image from a file in c# in the UWP format

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

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

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

不过!!!

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

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

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

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 的路径。该应用程序没有权限直接访问路径,需要通过存储文件通过文件代理。

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 sample and the Basic input sample 以获取旋转、缩放等示例