Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi i making more than 1000 picturebox in runtime in panel and now i want to print all these picturebox and this is my code but it print the last picturebox, what should i do to it print all picturebox one by one?
C#
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    a = Convert.ToInt32(textBox1.Text);
    for (int i = 0; i < a; i++)
    {
        Bitmap bp = new Bitmap(pic[s].Image, printDoc.DefaultPageSettings.PaperSize.Height, printDoc.DefaultPageSettings.PaperSize.Width);
        Rectangle rec = new Rectangle(label2.Left + 16, label2.Top + 12, label2.Width, label2.Height);
        e.Graphics.DrawImage(pic[s].Image, 0, 0);
        LinearGradientBrush lgb = new LinearGradientBrush(rec, Color.Red, Color.Red, LinearGradientMode.Vertical);
        e.Graphics.DrawString(q.ToString(), label2.Font, lgb, rec);
        bp.Dispose();
        printDoc.DefaultPageSettings.Landscape = true;
    }
}

With Respect
Posted

1 solution

Actually you are printing all the pictures on to of each other but at the endf only the last picture is visible.

Start by reading: An absolute beginner's guide to printing in .NET[^]

You want your PrintPage subroutine to be called once per picture - so what you do is if your current picture is not the last one, set e.HasMorePages = True to print another page. In that PrintPage subroutine, only print the current picture and increment the current picture counter.

// Declare this counter at class level...      
int currentPicture = 0;

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            a = Convert.ToInt32(textBox1.Text);

                Bitmap bp = new Bitmap(pic[ currentPicture ].Image, printDoc.DefaultPageSettings.PaperSize.Height, printDoc.DefaultPageSettings.PaperSize.Width);
                Rectangle rec = new Rectangle(label2.Left + 16, label2.Top + 12, label2.Width, label2.Height);
                e.Graphics.DrawImage(pic[ currentPicture ].Image, 0, 0);
                LinearGradientBrush lgb = new LinearGradientBrush(rec, Color.Red, Color.Red, LinearGradientMode.Vertical);
                e.Graphics.DrawString(q.ToString(), label2.Font, lgb, rec);
                bp.Dispose();
                printDoc.DefaultPageSettings.Landscape = true;
            

       if (currentPicture < a)
       { 
           // There are more pictures to print
           currentPicture +=1;
           e.HasMorePages = true;
       }
        }
 
Share this answer
 
v2
Comments
Avenger1 25-Feb-15 11:40am    
it didn't work
when i set "e.HasMorePages = True" and i clicked the button to print the print pages counter didn't stop and went more more but i selected just 10 pictures to print
Please help me to fix it
and that link didn't help me
With Respect
Duncan Edwards Jones 25-Feb-15 12:17pm    
if your current picture is not the last one, set e.HasMorePages = True

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900