打印列表<picturebox> - 每页一个图像</picturebox>

时间:2012-12-12 15:48:36

标签: c# winforms

我有一个 PictureBox列表。以下代码适用于单个PictureBox。无论纸张尺寸如何,我如何在新页面中打印每个PictureBox(或图像)?谢谢!

private void btnPrint_Click(object sender, EventArgs e)
{
   PrintDocument doc = new PrintDocument();
   doc.PrintPage += Doc_PrintPage;
   PrintDialog dlgSettings = new PrintDialog();

   dlgSettings.Document = doc;

   if (dlgSettings.ShowDialog() == DialogResult.OK)
   {
       doc.Print();
   }
}

private void Doc_PrintPage(object sender, PrintPageEventArgs e)
{
   float x = e.MarginBounds.Left;
   float y = e.MarginBounds.Top;
   Bitmap bmp = new Bitmap(picBox1.Width, picBox1.Height);

   //THIS IS OKAY FOR A SINGLE PICTURE BOX. 
   picBox1.DrawToBitmap(bmp, 
            new Rectangle(0, 0, picBox1.Width, 
            picBox1.Height));

   e.Graphics.DrawImage((Image)bmp, x, y);
}

1 个答案:

答案 0 :(得分:3)

尝试跟踪图像索引:

int bmpIndex = 0;
List<Bitmap> bmps = new List<Bitmap>();

void pd_BeginPrint(object sender, PrintEventArgs e) {
  bmpIndex = 0;
}

void pd_PrintPage(object sender, PrintPageEventArgs e) {
  e.Graphics.DrawImage(bmps[bmpIndex], new Point(e.MarginBounds.Left, 
                                                 e.MarginBounds.Top));

  ++bmpIndex;
  if (bmpIndex < bmps.Count) {
    e.HasMorePages = true;
  }
}