【发布时间】:2019-03-25 06:28:49
【问题描述】:
当我单击打印按钮时,程序会将图像保存到某个文件名。然后程序获取文件并打印它。但是当我第二次这样做时,即使打印完成,该文件也正在被另一个进程使用。有什么方法可以在再次按下打印按钮之前关闭文件?
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
string pathImage = Environment.CurrentDirectory + "\\chart1.png";
chart1.SaveImage(pathImage, System.Windows.Forms.DataVisualization.Charting.ChartImageFormat.Png);
Image newImage = Image.FromFile(pathImage);
Point ulCorner = new Point(50, 425);
e.Graphics.DrawImage(newImage, ulCorner);
}
private void button4_Click(object sender, EventArgs e)
{
if (printPreviewDialog1.ShowDialog() == DialogResult.OK)
{
printDocument1.Print();
}
}
【问题讨论】:
-
您需要致电
newImage.Dispose()或使用using (Image newImage ...。任何时候课程没有按照你的想法做,read the docs。 -
网站上已经有很多关于这个确切问题的问答。尝试使用搜索功能,输入您收到的确切错误消息和相关关键字,如“图像”或“保存”。一个这样的例子见标记的重复。
-
@PeterDuniho 虽然我想说的是你可能选择的所有骗子,但那个是最糟糕的。汉斯的另一个著名的斯巴达式答案......
标签: c#