how to print all images of flowlayoutpanel using c#

2k Views Asked by At

Can anyone help me to print all 10 images which are in a flowlayoutpanel using c# (Visual Studio .NET 2005 or 2008)

I do not have any idea how to do this?

1

There are 1 best solutions below

2
On

If you asking about WinForms FlowLayoutPanel and you are using PictureBox-es to display images, then you can try something like this:

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

Keep in mind that you might need to verify control types in FlowLayoutPanel, verify images count before starting printing, scale images and some other stuff.