2011-02-07 68 views

回答

0

如果你問的WinForms FlowLayoutPanel的,並且您使用的PictureBox-ES來顯示圖像,那麼你可以嘗試這樣的事:

private int imagesToPrintCount; 

private void PrintAllImages() 
{ 
    imagesToPrintCount = flowLayoutPanel1.Controls.Count; 
    PrintDocument doc = new PrintDocument(); 
    doc.PrintPage += Document_PrintPage; 
    PrintDialog dialog = new PrintDialog(); 
    dialog.Document = doc; 

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

private void Document_PrintPage(object sender, PrintPageEventArgs e) 
{ 
    e.Graphics.DrawImage(GetNextImage(), e.MarginBounds); 
    e.HasMorePages = imagesToPrintCount > 0; 
} 

private Image GetNextImage() 
{ 
    PictureBox pictureBox = (PictureBox)flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - imagesToPrintCount]; 
    imagesToPrintCount--; 
    return pictureBox.Image; 
} 

請記住,您可能需要驗證在FlowLayoutPanel的控制類型,在開始打印之前驗證圖像數量,縮放圖像和其他東西。

+0

我正面臨打印預覽的問題。你能幫我顯示來自flowlayout控制的所有圖像的打印預覽嗎? – james

+0

創建PrintPreviewDialog組件並將您的PrintDocument分配給預覽對話框的Document屬性。你可以用它來代替PrintDialog。但請記住,如果您要從預覽對話框打印,則需要重置imagesToPrintCount。正如我記得的,您可以使用文檔的BeginPrint事件。 –