locked
Downloading Pictureslibrary file to Bitmapimage to observablecollection => gridview

    Question

  • The problem I'm having is probably very simplistic, but I'm missing it. I am downloading images and putting them into the pictures library. When each image finishes downloading I want to add the image to a observablecollection which I have bound to a gridview, I had this working before but I was adding the images at the wrong point in the program (I was doing it on the progress check for the downloads, which made most of the images never show up in the gridview). In any case I decided the best way to do this was to add it to the async download task. The thought here is that each download task is separate from everything else. So when a download is completed it just adds that image to the collection and I'm good. 

    What I'm running into is, when I debug it shows that the file has been completely downloaded, I go to bind the image and it doesn't show in the gridview.  But I can open the image outside of the app, so it has completed.  I'm not 100% sure as to why but when I look at the debug watches and at the img object which I'm putting into the observablecollection.

    My code is below any help is greatly appreciated.


        public async void addimageobservcoll(IStorageFile ResultFile)
            {
                if (!ImageDisplay.IsOn)
                {
                    slrGridView.ItemsSource = null;
                }
                else
                {
                    try
                    {
                        string filename = ResultFile.Name.ToString();
                        StorageFile loadimagefile = await logconfig.SelectedlrDLFolder.GetFileAsync(filename);
                        BitmapImage img = new BitmapImage();
                        img = await loadbmpimage(loadimagefile);
                        if (img != null)
                        {
                            logconfig.completeddownloads++;
                            slrblog_gv_list.Add(new slrblog_gv() { Title = filename, Url = "", DLImage = img });
                            updatestats();
                            slrGridView.ItemsSource = slrblog_gv_list;
                        }
                        //else
                        //{
                        //    // This would be that the image exists so we are skipping it.
                        //    //if (logconfig.completeddownloads != 0 )
                        //    //    logconfig.completeddownloads--;
                        //}
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine(ex.Message);
                    }
                }
            }


            private static async Task<BitmapImage> loadbmpimage(StorageFile loadimagefile)
            {
                try
                {
                    BitmapImage bitmapImage = new BitmapImage();
                    FileRandomAccessStream stream = (FileRandomAccessStream)await loadimagefile.OpenAsync(FileAccessMode.Read);
                    bitmapImage.SetSource(stream);
                    return bitmapImage;
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                    return null;
                }
            }

    I've added what the img object has before I put it into the observable collection

    -        img    {Windows.UI.Xaml.Media.Imaging.BitmapImage}    Windows.UI.Xaml.Media.Imaging.BitmapImage
    -        base    {Windows.UI.Xaml.Media.Imaging.BitmapImage}    Windows.UI.Xaml.Media.Imaging.BitmapSource {Windows.UI.Xaml.Media.Imaging.BitmapImage}
    -        base    {Windows.UI.Xaml.Media.Imaging.BitmapImage}    Windows.UI.Xaml.Media.ImageSource {Windows.UI.Xaml.Media.Imaging.BitmapImage}
    +        base    {Windows.UI.Xaml.Media.Imaging.BitmapImage}    Windows.UI.Xaml.DependencyObject {Windows.UI.Xaml.Media.Imaging.BitmapImage}
            PixelHeight    250    int
            PixelWidth    250    int
    -        Static members        
    +        PixelHeightProperty    {Windows.UI.Xaml.DependencyProperty}    Windows.UI.Xaml.DependencyProperty
    +        PixelWidthProperty    {Windows.UI.Xaml.DependencyProperty}    Windows.UI.Xaml.DependencyProperty
            CreateOptions    None    Windows.UI.Xaml.Media.Imaging.BitmapCreateOptions
            DecodePixelHeight    0    int
            DecodePixelType    Physical    Windows.UI.Xaml.Media.Imaging.DecodePixelType
            DecodePixelWidth    0    int
    +        UriSource    null    System.Uri
    +        Static members        

    • Edited by Kyle Weir Saturday, August 9, 2014 12:25 AM
    Friday, August 8, 2014 8:45 PM

Answers

  • Well on the Positive side I realized that I messed up in my XAML. I had accidentally commented out the line in my gridview for the images. Spent 3 hours on this, dagh.

    Kyle Weir

    Saturday, August 9, 2014 12:50 AM

All replies

  • Well on the Positive side I realized that I messed up in my XAML. I had accidentally commented out the line in my gridview for the images. Spent 3 hours on this, dagh.

    Kyle Weir

    Saturday, August 9, 2014 12:50 AM
  • Hi Kyle Weir,

    At least you solve your problem.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, August 11, 2014 9:32 AM
    Moderator