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

标签 c# image uwp slideshow

我以前是一名 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 设置 BitmapImage 的源以及从 StorageFile 打开的流,而不是仅使用 StorageFile 的路径。应用程序没有权限直接访问 Path,需要通过 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# - 如何从 C# 文件中加载 UWP 格式的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52104377/

相关文章:

c# - Umbraco从7.2.8升级到7.3.0破坏了网站

ruby-on-rails - rails 3 : best way to preview image before upload

html - Facebook 的图像缩略图共享链接

.net - UWP使用.net标准库的引用问题

c# - ListView SelectedItems 绑定(bind) : why the list is always null

c# - 样式化 Xamarin.Forms DisplayAlert

c# - 什么会导致 ListView 中的透明滚动条错误?

c# - 如何使用 DateTime.Parse() 创建 DateTime 对象

c# - 在 Live SDK 中使用异步/等待

php - 保存图像仅在登录时可用