c# - 打印后删除文件
问题描述
我正在使用以下代码在临时路径上创建一个文件,然后在打印成功后将其删除。但是在打印后我尝试处理文件,当我尝试删除文件时,我仍然得到一个异常说“进程无法访问文件'Chart0.png',因为它正在被另一个进程使用。” 请帮忙。
我还尝试将删除代码放在 finally 块中,但仍然没有运气。
public static bool PrintAllCharts(Dictionary<string, ILightningChartInterface> charts)
{
DirectoryInfo info = null;
try
{
string FilePathWithoutFileName = string.Empty;
info = Directory.CreateDirectory(@"C:\TempCharts");
for (int i = 0; i < charts.Count; i++)
{
KeyValuePair<string, ILightningChartInterface> kp = charts.ElementAt(i);
FilePathWithoutFileName = info.FullName;
string FullPath = string.Format("{0}/Chart{1}.png", FilePathWithoutFileName, i.ToString());
kp.Value.SaveChartToFile(FullPath);
}
var files = Directory.GetFiles(FilePathWithoutFileName);
using (var pdoc = new PrintDocument())
{
using (var pdi = new System.Windows.Forms.PrintDialog { Document = pdoc, UseEXDialog = true })
{
if (pdi.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
pdoc.PrinterSettings = pdi.PrinterSettings;
pdoc.PrintPage += Pdoc_PrintPage;
foreach (var file in files)
{
pdoc.DocumentName = file;
pdoc.Print();
}
}
}
}
//Dispose the file after printing.
foreach(var file in files)
{
Image.FromFile(file).Dispose();
File.Delete(file); //This line gives an exception
}
foreach (DirectoryInfo dir in info.GetDirectories())
{
dir.Delete(true);
}
return true;
}
catch (Exception ex)
{
return false;
}
}
private static void Pdoc_PrintPage(object sender, PrintPageEventArgs e)
{
string file = ((PrintDocument)sender).DocumentName;
System.Drawing.Image img = System.Drawing.Image.FromFile(file);
Rectangle m = e.MarginBounds;
if ((double)img.Width / (double)img.Height > (double)m.Width / (double)m.Height) // image is wider
{
m.Height = (int)((double)img.Height / (double)img.Width * (double)m.Width);
}
else
{
m.Width = (int)((double)img.Width / (double)img.Height * (double)m.Height);
}
e.Graphics.DrawImage(img, m);
}
解决方案
问题在于你的Pdoc_PrintPage
方法。您正在使用以下行来读取文件:
System.Drawing.Image img = System.Drawing.Image.FromFile(file);
状态的文档FromFile
:
该文件保持锁定状态,直到图像被释放。
所以你真的应该这样写你的代码,以便在你完成后处理图像(并解锁文件):
string file = ((PrintDocument)sender).DocumentName;
using (System.Drawing.Image img = System.Drawing.Image.FromFile(file))
{
Rectangle m = e.MarginBounds;
if ((double)img.Width / (double)img.Height > (double)m.Width / (double)m.Height) // image is wider
{
m.Height = (int)((double)img.Height / (double)img.Width * (double)m.Width);
}
else
{
m.Width = (int)((double)img.Width / (double)img.Height * (double)m.Height);
}
e.Graphics.DrawImage(img, m);
}
请注意,您必须处理相同的图像实例。您当前在删除循环中有此代码:
Image.FromFile(file).Dispose();
这只是尝试加载文件的第二个副本,然后立即处理它。完成上述更改后,您还应该从删除循环中删除此行。
推荐阅读
- flutter - 由于内部错误,Andriod 模拟器关闭
- powerbi - 打开powerBI的问题
- typescript - 如何使`[key: string]: string`出现在NestJS Swagger生成中
- google-cloud-platform - 使用自定义服务帐户在 GCP 中创建 VM 时,KMS 权限出现 400 错误
- sql - Postgres - 从组中检索最新记录的最有效方法
- ruby-on-rails - Rails迁移,如何更改表列
- javascript - ReactJS 中的警告。组件正在更改要控制的非受控输入。这可能是由于值从 undefined 变为
- mysql - 如何在连接表中创建不可重复的连接
- python - 使用 python 输出作为另一个守护进程的输入
- amazon-web-services - 对象和类型的组合 any - Terraform