locked
Cannot display image which retireved from Local Folder RRS feed

  • Question

  • I have some images files(.png) stored in LocalFolder. I want to retrieve it and display in Image control.

    The code below has no compiled error but it did not work. Can anyone point out what wrong with my code. Both are not working. Method (2) : Bitmapimage is null ? event there is data in RandomAccess

    Method 1:

    private async void LoadAndDisplayImgFile(int ProdId)
             {
                 string strFilenm = "S-" + ProdId.ToString() + ".png";

                var folder_path = System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "ImgProd");

                StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(folder_path);

                StorageFile storagefile = await folder.GetFileAsync(strFilenm);

                StorageFile jpgFile = await folder.GetFileAsync(strFilenm);

                DisplayImg(jpgFile);
            
            }


       private async void DisplayImg(StorageFile ImgFile)
             {


                 IRandomAccessStream fileStream = await ImgFile.OpenAsync(FileAccessMode.Read);         

                var reader = new Windows.Storage.Streams.DataReader(fileStream.GetInputStreamAt(0));

                await reader.LoadAsync((uint)fileStream.Size);
     
                 byte[] pixels = new byte[fileStream.Size];

                reader.ReadBytes(pixels);

                InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream();

                DataWriter writer = new DataWriter(ms.GetOutputStreamAt(0));

                writer.WriteBytes(pixels as byte[]);
         
                 writer.StoreAsync().GetResults();

                BitmapImage image = new BitmapImage();

                image.SetSource(ms);
              
            
                await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                 {
                     Image1.Source = image;

                });              
             }

    Method 2:

     private async void LoadAndDisplayImgFile(int ProdId)
     {
        string strFilenm = "S-" + ProdId.ToString() + ".png";

      var folder_path = System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "ImgProd");

     StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(folder_path);

     StorageFile jpgFile = await folder.GetFileAsync(strFilenm);
      
     IRandomAccessStream fileStream = await jpgFile.OpenAsync(Windows.Storage.FileAccessMode.Read);

     fileStream.Seek(0);
                  
     BitmapImage bi = new BitmapImage();  
     bi.SetSource(fileStream);
     Image1.Source = bi;                  

                 
     }


    • Edited by Machine7 Sunday, December 21, 2014 2:41 PM
    Sunday, December 21, 2014 11:07 AM

Answers

  • The following implementation of the LoadAndDisplayImgFile works when called with ProdId = 1 provided that there is a valid .png file named "S-1.png" in the local folder:

    private async void LoadAndDisplayImgFile(int ProdId)
            {
                string strFilenm = "S-" + ProdId.ToString() + ".png";
    
                var folder_path = System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "ImgProd");
    
                StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(folder_path);
    
                StorageFile jpgFile = await folder.GetFileAsync(strFilenm);
    
                BitmapImage bitmapImage = new BitmapImage();
                FileRandomAccessStream stream = await jpgFile.OpenAsync(FileAccessMode.Read) as FileRandomAccessStream;
                bitmapImage.SetSource(stream);
                Image1.Source = bitmapImage;
            }
    
    <Image x:Name="Image1"/>

    Please remember to mark helpful posts as answer and/or helpful.
    Sunday, December 21, 2014 9:04 PM

All replies

  • There is a simple way to do this.

           private void SetImageFromInstalledLocation()
           {
               BitmapImage image = new BitmapImage(new Uri("ms-appx:///Assets/2.png"));
               img1.Source = image; 
           }
    
    
           private void SetImageFromIsolateStorage()
           {
               BitmapImage image = new BitmapImage(new Uri("ms-appdata:///local/Images/S1.png"));
               img2.Source = image; 
           }
    

    You can download my sample project form here.


    在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。

    Sunday, December 21, 2014 3:52 PM
  • The following implementation of the LoadAndDisplayImgFile works when called with ProdId = 1 provided that there is a valid .png file named "S-1.png" in the local folder:

    private async void LoadAndDisplayImgFile(int ProdId)
            {
                string strFilenm = "S-" + ProdId.ToString() + ".png";
    
                var folder_path = System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "ImgProd");
    
                StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(folder_path);
    
                StorageFile jpgFile = await folder.GetFileAsync(strFilenm);
    
                BitmapImage bitmapImage = new BitmapImage();
                FileRandomAccessStream stream = await jpgFile.OpenAsync(FileAccessMode.Read) as FileRandomAccessStream;
                bitmapImage.SetSource(stream);
                Image1.Source = bitmapImage;
            }
    
    <Image x:Name="Image1"/>

    Please remember to mark helpful posts as answer and/or helpful.
    Sunday, December 21, 2014 9:04 PM
  • Hi Magnus,

    Your code work.

    However, I don't seems to understand when I do some break point in the code at 3 and 5. I step thru in VS.

    stream show this data :

    CanRead  true
    CanWrite false
    position 0
    Size     389801


    bitmapImage Show this:

    CreateOption      None
    DecodePexelHeight 0
    DecodePixelType   0
    UriSource         null

    If bitmapImage is null or not base on the above? But in the end I got the image displayed.

    Thanks

      {

    1) BitmapImage bitmapImage = new BitmapImage();
               
    2) FileRandomAccessStream stream = await jpgFile.OpenAsync(FileAccessMode.Read) as FileRandomAccessStream;
               
    3) bitmapImage.SetSource(stream);
               
    4) Image1.Source = bitmapImage;

    5) }

    Tuesday, December 23, 2014 1:24 AM