BitmapImage - delete - cannot access the file because it is being used by another process

MadhuLi-9634 41 Reputation points
2021-08-20T16:32:18.07+00:00

Hi,

I am using this code to show images in a UWP app .

<Image
x:Name="imageHolder"
MaxWidth="{Binding Path=ViewportWidth, ElementName=ScrollViewerMain}"
MaxHeight="{Binding Path=ViewportHeight, ElementName=ScrollViewerMain}"
HorizontalAlignment="Center"
VerticalAlignment="Center" />

> imageHolder.Source = new BitmapImage(new Uri("ms-appdata:///local/Artifacts/Images/" + App.fileName));

And there is a option to delete that image. It uses a code like this. But most of the time it gives this message 'The process cannot access the file because it is being used by another process. '.
This is the delete code.

>                       var folder = await artiFolder.GetFolderAsync("Images");

>                         //check file exists and delete

>                         if (await StudentAppUtils.IfStorageItemExist(folder, App.fileName))

>                         {

>                             var file = await folder.GetFileAsync(App.fileName);

>                             await file.DeleteAsync(StorageDeleteOption.Default);

>                         }

I have found this solution for WPF. But not sure how to fix it for UWP.
https://stackoverflow.com/questions/16908383/how-to-release-image-from-image-source-in-wpf

Please let me know what is the fix for this.

Note: Earlier we used below code to set the image source and in that case we didn't have any issue with deleting the file. But when we used that code there seems to be a quality drop in the image (noticed with 4k images). So had to use the above code to fix that. But now getting the file deleting issue mentioned above.

              if (imageFolder != null)
                    {
                        StorageFile imageFile = await imageFolder.GetFileAsync(App.fileName);

                        if (imageFile != null)
                        {
                            using (var stream = await imageFile.OpenAsync(Windows.Storage.FileAccessMode.Read))
                            {
                                {
                                    BitmapImage bitmap = new BitmapImage();
                                    bitmap.SetSource(stream);
                                    imageHolder.Source = bitmap;
                                }
                            }
                        }

Thanks,
Madhu

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,861 Reputation points
    2021-08-23T02:34:27.583+00:00

    Hello, Welcome to Micorosoft Q&A,

    cannot access the file because it is being used by another process

    It's by-design, the image file was using by the image control when you delete it. if you want to delete used image file, you need dispose it at first

    private async void Button_Click(object sender, RoutedEventArgs e)  
    {  
        var folder = ApplicationData.Current.LocalFolder;  
        //check file exists and delete          
        var file = await folder.GetFileAsync("test.png");  
        DisposeImage();  
        await file.DeleteAsync(StorageDeleteOption.Default);  
      
    }  
    private void DisposeImage()  
    {  
        imageHolder.Source = null;  
        GC.Collect();  
    }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.