ARTICLE

Print the Window Form & Print the Image of Picture Box

Posted by Ghanashyam Nayak Articles | Printing in C# February 13, 2012
In this article you will learn how to print a Windows Forms form & how to print the image of a Picture box.
Reader Level:
Download Files:
 

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….

Login to add your contents and source code to this article
post comment
     

Hi Radhakrishnan G, Thank you. it is good to see that article is useful for you...

Posted by Ghanashyam Nayak May 21, 2012

Hello, I need your kind help. I have 1 task in Printing a winform's values. Client has given already printed paper with blank value. these values i need to fill in paper while printing. Pls give me code that can print only values at specific locations. that would be better if this can be possible directly from Current form or using Report Viewer. pls help me in this with code.

Posted by rikin shah May 12, 2012

Very Nice Illustration and a very usefull article. I was searching to make a export functionality to print the drawing entities in GDI to a image file. Now studying this article i have done it. Thank you..

Posted by Radhakrishnan G May 03, 2012

Thanks Sir

Posted by Rais Shaikh Mar 26, 2012

Thank you Abhi...

Posted by Ghanashyam Nayak Feb 15, 2012
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts