WPF“正由另一进程使用,因此该进程无法访问该文件”的解决方法

问题原因:

WPF 打开本地图片,同时另一个进程去访问这个图片;

BitmapImage bitmap = new BitmapImage();bitmap.BeginInit();bitmap.UriSource = new Uri(filePath);bitmap.EndInit();Image currentImage .Source = bitmap;

此时提升:正由另一进程使用,因此该进程无法访问该文件”

尝试了很多方法,都不成功,最终解决方案如下:

// 把图片写进 byte[] 缓存BinaryReader binaryReader = new BinaryReader(File.Open(filePath, FileMode.Open));FileInfo fileInfo = new FileInfo(filePath);byte[] bytes = binaryReader.ReadBytes((int)fileInfo.Length);binReader.Close();// 把byte[] 缓存作为图片资源BitmapImage bitmap = new BitmapImage();bitmap.BeginInit();bitmap.StreamSource = new MemoryStream(bytes);bitmap.EndInit();Image currentImage .Source = bitmap;

  

  

相关文章