在 windows 商店应用程序中从相机捕获后的图像预览在第 3 次后无法正常工作

Image preview after capturing from camera in windows store app not working after 3rd time

我正在使用 Media Capture Api 在我的 windows store metro 应用中拍摄照片。 正确捕获的照片首先保存在本地存储中,然后以位图图像显示。这是我的代码。

Windows.Media.Capture.MediaCapture captureManager;
 protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            captureManager = new MediaCapture();
            await captureManager.InitializeAsync();
            capturePreview.Source = captureManager;
            await captureManager.StartPreviewAsync();
            base.OnNavigatedTo(e);
        }

private async void CapturePhoto_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg();
                string fileName = "" + fittingdetail.FittingDetailId + ".jpg";
                StorageFile photoFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
                await captureManager.CapturePhotoToStorageFileAsync(imgFormat, photoFile);
                BitmapImage bitmapToShow = new BitmapImage(new Uri(photoFile.Path));
                imagePreivew.Source = bitmapToShow;  // show image on screen inside Image control defined in XAML
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

此代码工作正常问题是在第 3 次尝试后图像预览未更新它显示的是最后拍摄的图像。我检查本地存储文件夹图像在那里更新但不更新位图图像。我也像这样在 OnNavigatedFrom 中处理对象

 public void Dispose()
        {
            if (captureManager != null)
            {
                captureManager.Dispose();
                captureManager = null;
            }
        }

需要帮助我做错了什么。我不想在本地存储中复制文件我尝试过使用唯一名称它在那里工作正常。我也尝试在捕获文件后将其删除,但没有显示预览。

似乎 BitmapImage 特定路径的对象可能被缓存并且更新可能未被检测到。

我看到几个选项:

  • 每次使用不同的文件名
  • 根本不使用文件,而是捕获到内存流并从该流中加载 BitmapImage
  • 从文件流中加载 BitmapImage 而不是使用 Source URI 属性