none
Setting CacheMode="BitmapCache" on an Image sets the UriSource of a BitmapImage used in Image.Source to null

    Question

  • I am relying on BitmapImage objects and their UriSource properties in my view models to create images on screen. Now I set CacheMode="BitmapCache" on the Images that bind to my view models' exposed BitmapImage-typed property and it turns out that after the first time this is done - the UriSource of the BitmapImage gets set to null... This breaks my code and while I can implement a workaround - it is pretty annoying.

    Repro:

    <Page
        x:Class="App63.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App63"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Grid
            Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <ScrollViewer>
                <StackPanel
                    x:Name="panel">
                    <Image
                        x:Name="sourceImage"
                        Stretch="None"
                        Source="/Assets/SplashScreen.png" />
                    <Button
                        Content="Copy"
                        Click="Button_Click_1" />
                </StackPanel>
            </ScrollViewer>
        </Grid>
    </Page>
    

    code behind:

    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Media.Imaging;
    
    namespace App63
    {
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
    
            private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                panel.Children.Add(
                    new Image
                    {
                        CacheMode = new BitmapCache(),
                        Stretch = Stretch.None,
                        Source = sourceImage.Source
                    });
                panel.Children.Add(
                    new ContentControl
                    {
                        Content = ((BitmapImage)sourceImage.Source).UriSource
                    });
            }
        }
    }
    


    Filip Skakun

    Monday, November 05, 2012 10:39 PM

Answers

  • Here is a workaround, if you also set the CacheMode = “BitmapCache”, then the UriSource will never be NULL. The updated xaml:

        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">

            <ScrollViewer>

                <StackPanel

                    x:Name="panel">

                    <Image

                        CacheMode="BitmapCache"

                        x:Name="sourceImage"

                        Stretch="None"

                        Source="/Assets/SplashScreen.png" />

                    <Button

                        Content="Copy"

                        Click="Button_Click_1" />

                </StackPanel>

            </ScrollViewer>

        </Grid>

    Sample code:

    using Windows.UI.Xaml;

    using Windows.UI.Xaml.Controls;

    using Windows.UI.Xaml.Media;

    using Windows.UI.Xaml.Media.Imaging;

    namespace App63

    {

        public sealed partial class MainPage : Page

        {

            public MainPage()

            {

                this.InitializeComponent();

            }

            private void Button_Click_1(object sender, RoutedEventArgs e)

            {

                panel.Children.Add(

                    new Image

                    {

                        CacheMode = new BitmapCache(),

                        Stretch = Stretch.None,

                        Source = sourceImage.Source

                    });

                panel.Children.Add(

                    new ContentControl

                    {

                        Content = ((BitmapImage)sourceImage.Source).UriSource

                    });

            }

        }

    }

    Wednesday, December 19, 2012 6:33 PM

All replies

  • Hello,
     
    Thanks for your feedback, I will involve more experts to investigate it.
     
    Best regards,
    Jesse

    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Friday, November 09, 2012 3:34 AM
    Moderator
  • I am unable to repro the issue. Please email me a scale-down version of your project (entire solution as a zipped file): cts-enamulkh@live.com
    Monday, November 19, 2012 10:48 PM
  • Here is a workaround, if you also set the CacheMode = “BitmapCache”, then the UriSource will never be NULL. The updated xaml:

        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">

            <ScrollViewer>

                <StackPanel

                    x:Name="panel">

                    <Image

                        CacheMode="BitmapCache"

                        x:Name="sourceImage"

                        Stretch="None"

                        Source="/Assets/SplashScreen.png" />

                    <Button

                        Content="Copy"

                        Click="Button_Click_1" />

                </StackPanel>

            </ScrollViewer>

        </Grid>

    Sample code:

    using Windows.UI.Xaml;

    using Windows.UI.Xaml.Controls;

    using Windows.UI.Xaml.Media;

    using Windows.UI.Xaml.Media.Imaging;

    namespace App63

    {

        public sealed partial class MainPage : Page

        {

            public MainPage()

            {

                this.InitializeComponent();

            }

            private void Button_Click_1(object sender, RoutedEventArgs e)

            {

                panel.Children.Add(

                    new Image

                    {

                        CacheMode = new BitmapCache(),

                        Stretch = Stretch.None,

                        Source = sourceImage.Source

                    });

                panel.Children.Add(

                    new ContentControl

                    {

                        Content = ((BitmapImage)sourceImage.Source).UriSource

                    });

            }

        }

    }

    Wednesday, December 19, 2012 6:33 PM
  • Sorry, I was too busy and forgot to create that repro for you, but it seems like you managed to reproduce it after all.

    I can't test your workaround right now, but I'm afraid the BitmapCache could cause my image to blur during animation, since BitmapCache in Jupiter doesn't have an exposed RenderAtScale property as in other XAML UI platforms. It is an option of course, thanks.


    Filip Skakun

    Wednesday, December 19, 2012 6:49 PM