2012-12-12 24 views
1

我有一个PictureBox的列表。以下代码适用于单个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); 
} 

回答

2

建议保持跟踪:

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; 
    } 
} 
+0

+1你是一个明星! – Kaf