locked
Dynamic image displaying in WinRT RRS feed

  • Question

  • Sear Sir:

    I have a XAML file where I am displaying a dynamic image with MVVM design pattern. It works fine at run time, but my xaml design window gives me an error and unable to see what is going on. The reason is in my code I have

    ImageBrush imageBrush = new ImageBrush { ImageSource = new BitmapImage(new Uri(this.BaseUri, strImageName)) };


    As you see “this.BaseUri” is always null, because it is not running even I have correct image path.

    As I saw somewhere that with WinRT we have to read with a stream and display that image, but I was unable to find any sample with reads a stream from my current or my app  location.

    Any help will be very much appreciated.

    Best regards

    Agha Khan


    Agha Khan

    Tuesday, December 31, 2013 7:05 AM

Answers

  • Hi,

    Use these codes below:

      private async void Button_Click_1(object sender, RoutedEventArgs e)
            {
                BitmapImage bitmap = await GetImage();
                image.Source = bitmap;
            }
    See Asynchronous programming in winrt


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; 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.

    • Marked as answer by Anne Jing Tuesday, January 7, 2014 7:14 AM
    Wednesday, January 1, 2014 6:38 AM

All replies

  • Hello Agha

    If you are looking for image conversion from bitmap to stream or vice versa; you may have a look at my article if it helps you.

    Good luck!

    Arafat


    Tuesday, December 31, 2013 7:35 AM
  • Hi, Agha

    You can uses a file stream
    (obtained using a file picker, not shown) to load an image source by calling SetSourceAsync. See the link below to get more information and see a example in the link:

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.media.imaging.bitmapsource.setsourceasync.aspx<o:p></o:p>

    Best Wishes!



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; 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.


    • Edited by Anne Jing Wednesday, January 1, 2014 3:28 AM edit
    Wednesday, January 1, 2014 3:28 AM
  • Thanks for reply.

    I have an image in my assets folder but all the samples I looked are using StorageFile file = await GetStorageFilePath(url);

    How can I go my image file and use  stream. We are not using file picker.

    May be I am missing something.


    Agha Khan

    Wednesday, January 1, 2014 5:04 AM
  • Hi,

    To use a URI like ms-appx use the GetFileFromApplicationUriAsync | getFileFromApplicationUriAsync method :

      StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Image.png"));
               using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
               {
                   // Set the image source to the selected bitmap
                   BitmapImage bitmapImage = new BitmapImage();
                  
    
                  await bitmapImage.SetSourceAsync(fileStream);
                   
               }

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; 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.

    Wednesday, January 1, 2014 5:21 AM
  • Dear Anne jing:

    The code makes sense.

    But it never returns. I checked StorageFile and it is valid.

    public async static Task<BitmapImage> GetImage()
            {
                StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/A.png"));
                using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
                {
                    // Set the image source to the selected bitmap
                    Windows.UI.Xaml.Media.Imaging.BitmapImage bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
                    await bitmapImage.SetSourceAsync(fileStream);
                    return bitmapImage;  // it never get there
                }
            }

    somewhere in my code I write

     var t = Task.Factory.StartNew(() => GetImage());
                t.Wait();
                Image image = new Image();
                Task<BitmapImage> returnTask = t.Result;
            //    image.Source = returnTask;

    First I wanted to run the code but I was never able to (break point) last line of the task. I also realized I can't write (image.Source = returnTask). I am not sure the correct syntax.

    Thanks


    Agha Khan

    Wednesday, January 1, 2014 6:26 AM
  • Hi,

    Use these codes below:

      private async void Button_Click_1(object sender, RoutedEventArgs e)
            {
                BitmapImage bitmap = await GetImage();
                image.Source = bitmap;
            }
    See Asynchronous programming in winrt


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; 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.

    • Marked as answer by Anne Jing Tuesday, January 7, 2014 7:14 AM
    Wednesday, January 1, 2014 6:38 AM