Printing Windows Form in C#

Introduction

Vb.net has a PrintForm method but C# does not have inbuilt method for printing a Windows Form. 

The following procedure enables us to print a windows form at runtime in C#.net. The base concept involves the capture of  the screen image of a Form in jpeg format during runtime and printing the same on a event like Print button click.

Let's get started.

Create a new Windows Forms project in Visual Studio.

Add some simple controls such as a Label, TextBox, and Button control to the Form.

Now add a PrintDialog and a PrintDocument components in the Form by dragging and dropping them from Toolbox.

PrintDialog PrintDocument

In the code behind

Printing functionality is defined in the System.Drawing.Printing namespace. 

Include the following namespaces to your class: 

  1. using System.Drawing.Imaging;  
  2. using System.Drawing.Printing;
and import the following .dll for the necessary GDI functions
  1. [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
The following code is placed in the declaration section of the form. The BitBlt function performs a bit-block transfer of the color data corresponding to a rectangle of pixels from the specified source device context into a destination device context.
  1. private System.IO.Stream streamToPrint;  
  2. string streamType;  
  3. private static extern bool BitBlt  
  4. (  
  5.     IntPtr hdcDest, // handle to destination DC  
  6.     int nXDest, // x-coord of destination upper-left corner  
  7.     int nYDest, // y-coord of destination upper-left corner  
  8.     int nWidth, // width of destination rectangle  
  9.     int nHeight, // height of destination rectangle  
  10.     IntPtr hdcSrc, // handle to source DC  
  11.     int nXSrc, // x-coordinate of source upper-left corner  
  12.     int nYSrc, // y-coordinate of source upper-left corner  
  13.     System.Int32 dwRop // raster operation code  
  14. ); 

Select the PrintPage event of the PrintDocument component and include the following code in the event

  1. private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)  
  2. {  
  3.     System.Drawing.Image image = System.Drawing.Image.FromStream   
  4.     this.streamToPrint;  
  5.     int x = e.MarginBounds.X;  
  6.     int y = e.MarginBounds.Y;  
  7.     int width = image.Width;  
  8.     int height = image.Height;  
  9.     if ((width / e.MarginBounds.Width) > (height / e.MarginBounds.Height))  
  10.     {  
  11.         width = e.MarginBounds.Width;  
  12.         height = image.Height * e.MarginBounds.Width / image.Width;  
  13.     }  
  14.     else  
  15.     {  
  16.         height = e.MarginBounds.Height;  
  17.         width = image.Width * e.MarginBounds.Height / image.Height;  
  18.     }  
  19.     System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(x, y, width, height);  
  20.     e.Graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);  
  21. }  

Include the following code in the Print Click event handler

  1. private void btnPrint_Click(object sender, EventArgs e)  
  2. {  
  3.     Graphics g1 = this.CreateGraphics();  
  4.     Image MyImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, g1);  
  5.     Graphics g2 = Graphics.FromImage(MyImage);  
  6.     IntPtr dc1 = g1.GetHdc();  
  7.     IntPtr dc2 = g2.GetHdc();  
  8.     BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);  
  9.     g1.ReleaseHdc(dc1);  
  10.     g2.ReleaseHdc(dc2);  
  11.     MyImage.Save(@"c:\PrintPage.jpg", ImageFormat.Jpeg);  
  12.     FileStream fileStream = new FileStream(@"c:\PrintPage.jpg", FileMode.Open, FileAccess.Read);  
  13.     StartPrint(fileStream, "Image");  
  14.     fileStream.Close();  
  15.     if (System.IO.File.Exists(@"c:\PrintPage.jpg"))  
  16.     {  
  17.         System.IO.File.Delete(@"c:\PrintPage.jpg");  
  18.     }  
  19.  

And the StatrtPrint method to customize the PrintDialog and print the stored image

  1. public void StartPrint(Stream streamToPrint, string streamType)  
  2. {  
  3.     this.printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);  
  4.     this.streamToPrint = streamToPrint;  
  5.     this.streamType = streamType;  
  6.     System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();  
  7.     PrintDialog1.AllowSomePages = true;  
  8.     PrintDialog1.ShowHelp = true;  
  9.     PrintDialog1.Document = printDoc;  
  10.     DialogResult result = PrintDialog1.ShowDialog();  
  11.     if (result == DialogResult.OK)  
  12.     {  
  13.         printDoc.Print();  
  14.         //docToPrint.Print();  
  15.     }  
  16. }

The captured image is saved in jpeg format in the defined location.When the print functionality is used throughout an application and the image is not required to be stored, the existing image file is deleted and the new one created is streamed to print.If the image is required to be stored, the filename can be specified at runtime and stored in the given path.

On running the application,the form is displayed as in the image below

print Windows Forms in C#

On click of  the Print button, the Print dialog is displayed.

print Windows Forms in C#


Summary

In this article, I demonstrated how to print a Windows Forms in C# using GDI+.


Similar Articles