【问题标题】:Image printing, file in use by another process [duplicate]图像打印,文件正在被另一个进程使用[重复]
【发布时间】: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#


【解决方案1】:

可能是因为你没有发布图片:

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);
    using (var newImage = Image.FromFile(pathImage))
    {
        Point ulCorner = new Point(50, 425);
        e.Graphics.DrawImage(newImage, ulCorner);
    }
}

记住ImageIDisposable

【讨论】:

  • 非常感谢!这帮助我解决了我的问题!
  • @Christof,您可以通过单击答案左上角的绿色复选标记来感谢人们。
猜你喜欢
  • 2013-08-12
  • 1970-01-01
  • 1970-01-01
  • 2011-04-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多