【发布时间】: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#