none
[UWP][C#]Exception Loading images with stream

    Question

  • 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?



    Sunday, March 19, 2017 12:57 AM

All replies