【问题标题】:Why can change BitmapImage source only once?为什么只能更改一次 BitmapImage 源?
【发布时间】:2016-09-18 10:03:09
【问题描述】:

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

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>

代码

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

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

【问题讨论】:

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


    【解决方案1】:

    在创建 BitmapImage 时,将 CreateOptions 设置为 IgnoreImageCache

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

    【讨论】:

    • 替代方案:在替换文件之前将 Ellipse.Fill 更改为 null,然后用新文件创建新画笔。
    • 这真的解决了问题!我猜是这样的,但根本不知道该怎么做。 (甚至尝试过 await Task.Delay(5000)...哈哈)谢谢!
    • 附言。试图改变椭圆等。为空,但那些根本没有帮助:-)
    • "在替换文件之前。" 我猜是因为.CopyAndReplaceAsync() 它以某种方式确定 Image 不再存在,所以它在缓存中使用了一个。
    【解决方案2】:

    尝试添加 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()
    }
    

    【讨论】:

      猜你喜欢
      • 2023-01-19
      • 1970-01-01
      • 2021-05-16
      • 1970-01-01
      • 1970-01-01
      • 2019-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多