点击一个按钮打印多个图像打印所有winforms

时间:2011-12-09 07:30:17

标签: c# winforms printing

我已经在磁盘中创建了多个图像。我想点击打印所有按钮并打印所有图像一次打印。

public void PrintGraph()
{
    PrintDocument pd = new PrintDocument();

    pd.PrintPage += new PrintPageEventHandler(this.PrintImageHandler);

    PrintDialog MyPrintDialog = new PrintDialog();

    if (MyPrintDialog.ShowDialog() == DialogResult.OK)
    {
        pd.Print();
    }
    myprintDocument.Dispose();
}

处理程序如下:

private void PrintImageHandler(object sender, PrintPageEventArgs ppeArgs)
{
    Graphics g = ppeArgs.Graphics;
    for (int i = 0; i < lstAllImages.Count; i++)
    {
        Image objimage = Image.FromFile(lstAllloadImages[i].ToString());
        g.DrawImage(objimage, 0, 0, objimage.Width, objimage.Height);
    } // Draw Image using the DrawImage method 
}

仅打印一张图像。 我想要点击打印所有按钮打印多个图像。

1 个答案:

答案 0 :(得分:2)

作为事件名称args对象建议(PrintPageEventArgs),将为您要生成的每个调用一次此事件处理程序。如果将HasMorePages属性设置为true,则会再次调用新页面。

在这个事件中,您只能控制单个页面上显示的内容,因此您当前的代码将所有图像叠加在一起(可能是它们的大小相同,或者最大的是最后一个,或者你已经注意到在最后一张图像的侧面或底部出现了一些早期图像。)

因此,您必须使用某些外部字段跟踪您想要绘制的图像。例如。你有:

if (MyPrintDialog.ShowDialog() == DialogResult.OK)
{
    currentPage = 0;
    pd.Print();
}

然后在你的事件处理程序中:

int currentPage;

private void PrintImageHandler(object sender, PrintPageEventArgs ppeArgs)
{
    Graphics g = ppeArgs.Graphics;
    Image objimage = Image.FromFile(lstAllOperatorloadImages[currentPage].ToString());
    g.DrawImage(objimage, 0, 0, objimage.Width, objimage.Height);
    currentPage++;
    ppeArgs.HasMorePages = currentPage <  lstAllOperatorloadImages.Count;
}

要在每页打印两张图片,我可能会这样做:

private void PrintImageHandler(object sender, PrintPageEventArgs ppeArgs)
{
    Graphics g = ppeArgs.Graphics;
    Image objimage = Image.FromFile(lstAllOperatorloadImages[currentPage].ToString());
    g.DrawImage(objimage, 0, 0, objimage.Width, objimage.Height);
    currentPage++;
    if(currentPage < lstAllOperatorloadImages.Count)
    {
        objimage = Image.FromFile(lstAllOperatorloadImages[currentPage].ToString());
        g.DrawImage(objimage, 0, 600, objimage.Width, objimage.Height);
        currentPage++;
    }
    ppeArgs.HasMorePages = currentPage <  lstAllOperatorloadImages.Count;
}