2

Please, help is needed. In this code have to be stupid mistake but what it is? If I run this very simple code I can load and change bitmap image only once. At first time it runs ok but if I like to change the image again (press the button) the first image remains. Why?

XAML

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel>
        <Button x:Name="AddProfilePicture"  HorizontalAlignment="Center" Click="AddProfilePicture_Click">
            <Grid Width="200" Height="200">
                <Ellipse Width="200" Height="200">
                    <Ellipse.Fill>
                        <ImageBrush x:Name="ImageBrush_ProfilePicture" Stretch="UniformToFill"/>
                    </Ellipse.Fill>
                </Ellipse>
            </Grid>
        </Button>
    </StackPanel>
</Grid>

CODE

    private async void AddProfilePicture_Click(object sender, RoutedEventArgs e)
    {
        FileOpenPicker profilePictureFilePicker = new FileOpenPicker();
        profilePictureFilePicker.ViewMode = PickerViewMode.Thumbnail;
        profilePictureFilePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
        profilePictureFilePicker.FileTypeFilter.Add(".jpg");
        StorageFile userSelectedProfilePicture = await profilePictureFilePicker.PickSingleFileAsync();
        //MAKE SURE U HAVE THIS FILE (ProfilePicture.jpg) IN FOLDER ALREADY....
        StorageFile destinationFileImage = await StorageFile.GetFileFromPathAsync(ApplicationData.Current.LocalFolder.Path + @"\" + "ProfilePicture.jpg");
        await userSelectedProfilePicture.CopyAndReplaceAsync(destinationFileImage);
        Uri profilePictureBitmapURI = new Uri(ApplicationData.Current.LocalFolder.Path + @"\" + "ProfilePicture.jpg");
        BitmapImage profilePictureBitmap = new BitmapImage(profilePictureBitmapURI);
        ImageBrush_ProfilePicture.ImageSource = profilePictureBitmap;
    }

This code doesn't have any extra check. It means the image 'ProfilePicture.jpg' have to be in a folder before running app. Code works well at first run but on the second run (press the button again and selecting new picture) cannot change output on screen even the source change in folder.

Weissu
  • 409
  • 3
  • 15

2 Answers2

1

While creating BitmapImage set CreateOptions to IgnoreImageCache.

var profilePictureBitmap = new BitmapImage(profilePictureBitmapURI) {CreateOptions = BitmapCreateOptions.IgnoreImageCache};
Tundy
  • 165
  • 13
  • **Alternative:** Change Ellipse.Fill to null before replacing file and then create new brush with new file. – Tundy Sep 18 '16 at 12:12
  • This really solve the problem! I guess something like this but didn't know at all how to do it. (even tried await Task.Delay(5000)... HAH) Thx! – Weissu Sep 18 '16 at 12:26
  • PS. Tried to change Ellipse ect. to null but those didn't helps at all :-) – Weissu Sep 18 '16 at 12:27
  • 1
    _"before replacing the file."_ I guess thah because of `.CopyAndReplaceAsync()` it somehow determine that Image doesn't exist anymore so it's using one in cache. – Tundy Sep 18 '16 at 12:32
0

Try to add the Load() method and see if that helps:

 private async void AddProfilePicture_Click(object sender, RoutedEventArgs e)
{
    FileOpenPicker profilePictureFilePicker = new FileOpenPicker();
    profilePictureFilePicker.ViewMode = PickerViewMode.Thumbnail;
    profilePictureFilePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
    profilePictureFilePicker.FileTypeFilter.Add(".jpg");
    StorageFile userSelectedProfilePicture = await profilePictureFilePicker.PickSingleFileAsync();
    //MAKE SURE U HAVE THIS FILE (ProfilePicture.jpg) IN FOLDER ALREADY....
    StorageFile destinationFileImage = await StorageFile.GetFileFromPathAsync(ApplicationData.Current.LocalFolder.Path + @"\" + "ProfilePicture.jpg");
    await userSelectedProfilePicture.CopyAndReplaceAsync(destinationFileImage);
    Uri profilePictureBitmapURI = new Uri(ApplicationData.Current.LocalFolder.Path + @"\" + "ProfilePicture.jpg");
    BitmapImage profilePictureBitmap = new BitmapImage(profilePictureBitmapURI);
    ImageBrush_ProfilePicture.ImageSource = profilePictureBitmap;
    ImageBrush_ProfilePicture.Load()
}
Shannon Holsinger
  • 2,293
  • 1
  • 15
  • 21