我正在像下面这样加载图片
XAML
<Image Stretch="None" Grid.Row="16" Height="70" HorizontalAlignment="Left" Name="imgThumbnail" VerticalAlignment="Top" Width="70" Grid.RowSpan="3" Margin="133,1,0,0" Grid.Column="2" Grid.ColumnSpan="2" />
CodeBehind
if (Path.GetFileNameWithoutExtension(filePath).ToLower().Contains(slugName.ToLower() + "_70x70"))
{
imgThumbnail.BeginInit();
imgThumbnail.Stretch = Stretch.UniformToFill;
imgThumbnail.Source = new BitmapImage(new Uri(filePath));
imgThumbnail.EndInit();
count = count + 1;
}
以上代码运行正常,现在我有一个删除按钮放在我的缩略图旁边,如果调用删除按钮,我应该从源位置删除所有图像。
以下是删除图像文件的代码
internal int Remove(string slugName, DirectoryInfo outputFolder)
{
Helper.MetadataView.imgThumbnail.Source = null;
foreach (string filePath_ToBeDeleted in filePathList_ToBeDeleted)
{
if (File.Exists(filePath_ToBeDeleted))
{
Helper.MetadataView.imgThumbnail.IsEnabled = false;
File.Delete(filePath_ToBeDeleted);
count += 1;
}
}
return count;
}
return 0; // slugName == null
}
我试图将来源设为null并删除,但是会抛出以下异常:
由于另一个进程正在使用,因此无法访问文件'\serv1\Dev\Images\730_Test4_0406_70x70.jpg'。
我不确定如何处理,请有经验的人指导一下。
imgThumbnail
是什么类型? - Gjeltema