Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,
I am working on a windows project. where i have a large no. of images.
I show these images in a picture box. I have a button to print this picture box. Now my problem is if The size of picture Box is increase then all pictures not shown on page.If size of picture box is more than page size then I want to display rest images on another page. Below is the code

C#
private void btn_prnt_Click(object sender, EventArgs e)
      {
          previewDlg = new PrintPreviewDialog();
          PrintDocument pd = new PrintDocument();
          //Add print-page event handler
          pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
          pd.Print();
      }
public void pd_PrintPage(object sender, PrintPageEventArgs ev)
      {
          float x = ev.MarginBounds.Left - 50;
          float y = ev.MarginBounds.Top - 70;
          float pageheight = ev.MarginBounds.Height;
          Bitmap bmp = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
          int imgWidth=this.pictureBox1.Width;
          int imght = ev.MarginBounds.Height;
              this.pictureBox1.DrawToBitmap(bmp, new Rectangle(0, 0, imgWidth, imght));
              ev.Graphics.DrawImage((Image)bmp, x, y);
          }


how i achieve this. Thanks
Posted

1 solution

C#
Bitmap myBitmap1 = new Bitmap(myPicturebox.Width, myPicturebox.Height);

Now Print this picturebox image.

myPicturebox.DrawToBitmap(myBitmap1, new Rectangle(0, 0, myPicturebox.Width, myPicturebox.Height));

e.Graphics.DrawImage(myBitmap1, 0, 0);
Then dispose the bitmap to release the resources.
Now create the objects of the PrintDocument & PrintDialog.
System.Drawing.Printing.PrintDocument myPrintDocument1 = new System.Drawing.Printing.PrintDocument();
PrintDialog myPrinDialog1 = new PrintDialog();
Now create the PagePrint event of PrintDocument

myPrintDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(myPrintDocument2_PrintPage);
Then assing the PrintDocument object to PrintDialog.

myPrinDialog1.Document = myPrintDocument1;
Finally it will print the Image which is in the picturebox.

if (myPrinDialog1.ShowDialog() == DialogResult.OK)
{
               myPrintDocument1.Print();
}
The above code will ask you the printer name in which you can print the picture.

The the full code for printing the picture.

private void myPrintDocument2_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Bitmap myBitmap1 = new Bitmap(myPicturebox.Width, myPicturebox.Height);
            myPicturebox.DrawToBitmap(myBitmap1, new Rectangle(0, 0, myPicturebox.Width, myPicturebox.Height));
            e.Graphics.DrawImage(myBitmap1, 0, 0);
            myBitmap1.Dispose();
        }
        private void btnPrintPicture_Click(object sender, EventArgs e)
        {
            System.Drawing.Printing.PrintDocument myPrintDocument1 = new System.Drawing.Printing.PrintDocument();
            PrintDialog myPrinDialog1 = new PrintDialog();
            myPrintDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(myPrintDocument2_PrintPage);
            myPrinDialog1.Document = myPrintDocument1;
 
            if (myPrinDialog1.ShowDialog() == DialogResult.OK)
            {
               myPrintDocument1.Print();
            }
        }
 
Share this answer
 
v2
Comments
KapilBhati 26-May-15 8:31am    
Thanks for your answer . now i'll try this code.
KapilBhati 26-May-15 8:41am    
not useful same problem.

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