0

I get the error: "An exception of type 'System.ArgumentException' occurred in App2.exe but was not handled in user code, Additional information: Value does not fall within the expected range".

This is when trying to load an image into my app. I have the code below to load the image from my database. The filename variable returns: "Y:\\Pictures\\Cake.jpg" which is the image path im trying to load from. Any help is appreciated, Thanks.

            string FilePath = @"Y:\Pictures\";
            FileName = FilePath + ms.RecipeImage;
            StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(FileName, UriKind.Absolute));
            IRandomAccessStream filestream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
            BitmapImage bmpImage = new BitmapImage();
            bmpImage.SetSource(filestream);
            image.Source = bmpImage;

And to save to database:

public async void imageButton_Click(object sender, RoutedEventArgs e)
    {
        FileOpenPicker open = new FileOpenPicker();
        open.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
        open.ViewMode = PickerViewMode.Thumbnail;

        // Filter to include a sample subset of file types
        open.FileTypeFilter.Clear();
        open.FileTypeFilter.Add(".bmp");
        open.FileTypeFilter.Add(".png");
        open.FileTypeFilter.Add(".jpeg");
        open.FileTypeFilter.Add(".jpg");

        // Open a stream for the selected file
        StorageFile file = await open.PickSingleFileAsync();

        // Ensure a file was selected
        if (file != null)
        {
            // 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();
                bitmapImage.DecodePixelHeight = 200;
                bitmapImage.DecodePixelWidth = 200;

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

        }          
    }
2
  • Is it possible, that exception has a line of code associated with it or may be even a stack trace? Commented Dec 20, 2015 at 20:48
  • It's associated with the line: StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(FileName, UriKind.Absolute)); Commented Dec 20, 2015 at 20:51

1 Answer 1

1

According to this page: https://msdn.microsoft.com/de-de/library/windows/apps/windows.storage.storagefile.getfilefromapplicationuriasync you can use GetFileFromApplicationUriAsync only with these URI-formats "ms-appx://"- or "ms-appdata://". You should be able to load ressources with this method from the assets folder for example.

EDIT: You cannot access all files and folders (https://msdn.microsoft.com/en-us/library/windows/apps/mt188700.aspx). But in your case this should work (if the files you want to load are living inside the my pictures folder).

var filestream = await Windows.Storage.KnownFolders.PicturesLibrary.OpenStreamForReadAsync("Screenshots\\Screenshot (1).png");
InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
await filestream.CopyToAsync(ras.AsStreamForWrite());

BitmapImage bmpImage = new BitmapImage();
bmpImage.SetSource(ras);

image.Source = bmpImage;

Don't forget to check the Pictures Library checkbox in your package.appxmanifest.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help, but can I also ask if you would know how I could load it from just My Pictures folder?
Thank you SOOOOOOOOOOO much, i've wasted a day on this but you helped a lot :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.