Print the Window Form & Print the Image of Picture Box


Let's first look at the form; see the following image :

01_Print_Form_Picture.png

Now the code:

In the form there are two options. First one is to print the whole from & the other one is to print the picture.

Let us see first how to print the picture.

First of all we have to create an object of Bitmap & its size is as picturebox width & height.

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();

            }

        }

 
See the following images :

02_Print_Form_Picture.png

03_Print_Form_Picture.png

04_Print_Form_Picture.png

The same code will be used for the whole form but instead of the pictureboc object it will use this & using the width & height, it will print the whole form.

The whole code for printing the form:

private void myPrintDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)

        {

            Bitmap myBitmap2 = new Bitmap(this.Width, this.Height);

            this.DrawToBitmap(myBitmap2, new Rectangle(0, 0, this.Width, this.Height));

            e.Graphics.DrawImage(myBitmap2, 0, 0);

            myBitmap2.Dispose();

        }

        private void btnPrintForm_Click(object sender, EventArgs e)

        {

            System.Drawing.Printing.PrintDocument myPrintDocument2 = new System.Drawing.Printing.PrintDocument();

            PrintDialog myPrinDialog2 = new PrintDialog();

            myPrintDocument2.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(myPrintDocument1_PrintPage);

            myPrinDialog2.Document = myPrintDocument2;

 

            if (myPrinDialog2.ShowDialog() == DialogResult.OK)

            {

                myPrintDocument2.Print();

            }

        }

See the following images :

05_Print_Form_Picture.png

06_Print_Form_Picture.png

Hope this is clear to all of You….

Up Next
    Ebook Download
    View all
    Learn
    View all