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

后端 未结 5 780
太阳男子
太阳男子 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: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();
    

提交回复
热议问题