为什么我的图像不能打印到PrintDocument?

时间:2013-04-30 14:14:55

标签: c# winforms

我在“C://”中有位图,其名称为“1.bmp”,“2. bmp”,“3. bmp”等,我正在尝试打印这些图像,但打印文档为空(图像的路径正确)

这是我的代码:

private void button3_Click_1(object sender, EventArgs e)
{
    PrintDocument pd = new PrintDocument();
    for (int indice = 0; indice < nPaginasPDF + 1; indice++)
    {
        pd.PrintPage += new PrintPageEventHandler(Print_Page);
    }
    PrintPreviewDialog dlg = new PrintPreviewDialog();

    dlg.Document = pd;
    dlg.ShowDialog();
    pd.Print();
}       

private void Print_Page(object o, PrintPageEventArgs e)
{
    nPaginasImpressas++;
    System.Drawing.Image i = System.Drawing.Image.FromFile("C:\\" + nPaginasImpressas + ".bmp");
    Point p = new Point(891, 1350);
    e.Graphics.DrawImage(i, p);
}

1 个答案:

答案 0 :(得分:2)

好的,因此打印页面的过程利用PrintPageEventArgs类,而不是多次附加事件。请考虑以下代码:

private void button3_Click_1(object sender, EventArgs e)
{
    PrintDocument pd = new PrintDocument();
    pd.PrintPage += new PrintPageEventHandler(Print_Page);
    PrintPreviewDialog dlg = new PrintPreviewDialog();

    dlg.Document = pd;
    dlg.ShowDialog();
    pd.Print();
}       

private void Print_Page(object o, PrintPageEventArgs e)
{
    nPaginasImpressas++;
    System.Drawing.Image i = System.Drawing.Image.FromFile("C:\\" + nPaginasImpressas + ".bmp");
    Point p = new Point(0, 0);
    e.Graphics.DrawImage(i, p);

    e.HasMorePages = File.Exists("C:\\" + (nPaginasImpressas + 1) + ".bmp");
}

此代码应该允许您打印多个页面。但请注意Point的变化对我来说是非常可疑的,然后是HasMorePages的杠杆。