为什么只能更改BitmapImage源一次?

时间:2016-09-18 10:03:10

标签: c# windows-10-universal bitmapimage storagefile fileopenpicker

请,需要帮助。在这段代码中必须是愚蠢的错误,但它是什么?如果我运行这个非常简单的代码,我只能加载和更改位图图像一次。第一次运行正常,但如果我想再次更改图像(按下按钮),第一张图像仍然存在。为什么呢?

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;
    }

此代码没有任何额外检查。这意味着图像&#39; ProfilePicture.jpg&#39;必须在运行应用程序之前在文件夹中。代码在首次运行时运行良好,但在第二次运行时(再次按下按钮并选择新图片)即使文件夹中的源更改,也无法更改屏幕上的输出。

2 个答案:

答案 0 :(得分:1)

创建 BitmapImage 时,将 CreateOptions 设置为 IgnoreImageCache

var profilePictureBitmap = new BitmapImage(profilePictureBitmapURI) {CreateOptions = BitmapCreateOptions.IgnoreImageCache};

答案 1 :(得分:0)

尝试添加Load()方法,看看是否有帮助:

 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()
}