Load a BitmapSource and save using the same name in WPF -> IOException

后端 未结 5 697
太阳男子
太阳男子 2020-12-21 00:53

When I try to save a BitmapSource that I loaded earlier, a System.IO.IOException is thrown stating another process is accessing that file and the filestream can

相关标签:
5条回答
  • 2020-12-21 01:12

    Add the following line to your loading code:

    image.CacheOption = BitmapCacheOption.OnLoad;
    

    This will load open the file, read it into memory, and close it all during image.EndInit. The default of BitmapCacheOption.Default has the odd behavior of opening the file, reading it into memory, but not yet closing it during image.EndInit.

    0 讨论(0)
  • 2020-12-21 01:14

    Here is another solution, based upon the original loading code:

    var image = new BitmapImage();
    image.BeginInit();
    
    // overwrite cache if already exists, to refresh image
    image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
    // load into memory and unlock file
    image.CacheOption = BitmapCacheOption.OnLoad;
    
    image.UriSource = uri;
    if (decodePixelWidth > 0) image.DecodePixelWidth = decodePixelWidth;
    image.EndInit();
    
    0 讨论(0)
  • 2020-12-21 01:21

    Now i'm not sure if this can be applied to BitmapImage but i had a very similar problem with saving a modified image to the original file in GDI+ here

    The method of loading the image from file keeps a lock open on the file until the image object is disposed.

    Maybe it's the same thing with bitmapimage.urisource. Without playing around could you copy the image in memory and dispose the original thus unlocking the file?

    0 讨论(0)
  • 2020-12-21 01:28

    Setting CacheOption to BitmapCacheOption.OnLoad, will not solve your problem. I think there is a bug, but I had the same problem. Finally i loaded my image to a memory stream and disposed the BitmapImage before saving the image to the file.

    0 讨论(0)
  • 2020-12-21 01:29

    Inspired by the comments I got on this issue, I solved the problem by reading all bytes into a memorystream and using it as the BitmapImage's Sreamsource.

    This one works perfectly:

    if (File.Exists(filePath))
    {
        MemoryStream memoryStream = new MemoryStream();
    
        byte[] fileBytes = File.ReadAllBytes(filePath);
        memoryStream.Write(fileBytes, 0, fileBytes.Length);
        memoryStream.Position = 0;
    
        image.BeginInit();
        image.StreamSource = memoryStream;
    
        if (decodePixelWidth > 0)
            image.DecodePixelWidth = decodePixelWidth;
    
        image.EndInit();
    }
    
    0 讨论(0)
提交回复
热议问题