dimanche 20 décembre 2015

Loading image from SQLite c# Windows App

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;
            }

        }          
    }

Aucun commentaire:

Enregistrer un commentaire