【问题标题】:C# System.IO.IOException 'The proces cannot access the file 'photolocation' because it is being used by another process [duplicate]C#System.IO.IOException'该进程无法访问文件'照片位置',因为它正被另一个进程使用[重复]
【发布时间】:2020-06-15 02:17:27
【问题描述】:

我使用这个函数来加载照片:

private void Load_Photo()
{
   BitmapImage bi1 = new BitmapImage();
   bi1.BeginInit();
   bi1.UriSource = new Uri(photolocation, UriKind.Absolute);
   bi1.EndInit();

   if (bi1 != null)
   {
     Image_Data = bi1;
   }
}

然后我想保存同一张照片,它给我一个错误提示:System.IO.IOException '进程无法访问文件'photolocation',因为它正在被另一个进程使用。

private void Save_Photo_To_JPG()
{
   var encoder = new JpegBitmapEncoder();

   encoder.Frames.Add(BitmapFrame.Create(Image_Data));

   using (var filestream = new FileStream(photolocation, FileMode.Create))

   encoder.Save(filestream);
}

我该如何解决这个问题?

【问题讨论】:

  • 您要覆盖现有文件还是创建新文件/文件夹?您正在使用 Create 将覆盖现有文件,但现有文件仍处于打开状态。
  • 注意缩进。如果你不打算缩进 encoder.Save(),你应该假设你的 using 语句需要大括号。

标签: c#


【解决方案1】:

这对我有用:

using (FileStream fs = new FileStream(path, FileMode.Open))
{
    bitmap.BeginInit();
    bitmap.StreamSource = fs;
    bitmap.CacheOption = BitmapCacheOption.OnLoad;
    bitmap.EndInit();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 2023-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多