本文介绍了[UWP] [C#]异常使用流加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

namespace App1
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            ShowFlag();
        }

        private async void ShowFlag()
        {
            string filename = "usa.jpg";
            Windows.Storage.StorageFile sampleFile = await Windows.Storage.KnownFolders.PicturesLibrary.GetFileAsync(filename);
            var img = await LoadImage(sampleFile);
            MyImage.Source = img;
        }

        private static async Task<BitmapImage> LoadImage(StorageFile file)
        {
            BitmapImage bitmapImage = new BitmapImage();
            FileRandomAccessStream stream = (FileRandomAccessStream)await file.OpenAsync(FileAccessMode.Read);
            bitmapImage.SetSource(stream);
            return bitmapImage;

        }
    }
}
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Image x:Name="MyImage"
               Stretch="None"></Image>
    </Grid>

非常简单的代码。为什么我会被例外?

Very simple code. Why I am getting exception?




推荐答案


这篇关于[UWP] [C#]异常使用流加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 04:53