SIGN UP MEMBER LOGIN:    
ARTICLE

Printing Windows Form in C#

Posted by Rajalakshmi Articles | Printing in C# February 01, 2008
This article shows how to print a Windows Forms in C# at run-time.
Reader Level:
Download Files:
 

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.

1.gif

In the code behind

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

Include the following namespaces to your class: 

using System.Drawing.Imaging;
using System.Drawing.Printing;

and import the following .dll for the necessary GDI functions

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

private System.IO.Stream streamToPrint;

string streamType;

private static extern bool BitBlt

(

    IntPtr hdcDest, // handle to destination DC

    int nXDest, // x-coord of destination upper-left corner

    int nYDest, // y-coord of destination upper-left corner

    int nWidth, // width of destination rectangle

    int nHeight, // height of destination rectangle

    IntPtr hdcSrc, // handle to source DC

    int nXSrc, // x-coordinate of source upper-left corner

    int nYSrc, // y-coordinate of source upper-left corner

    System.Int32 dwRop // raster operation code

);

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


private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)

{

    System.Drawing.Image image = System.Drawing.Image.FromStream
    this.streamToPrint;

    int x = e.MarginBounds.X;

    int y = e.MarginBounds.Y;

    int width = image.Width;

    int height = image.Height;

    if ((width / e.MarginBounds.Width) > (height / e.MarginBounds.Height))

    {

        width = e.MarginBounds.Width;

        height = image.Height * e.MarginBounds.Width / image.Width;

    }

    else

    {

        height = e.MarginBounds.Height;

        width = image.Width * e.MarginBounds.Height / image.Height;

    }

    System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(x, y, width, height);

    e.Graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
}

Include the following code in the Print Click event handler


private void btnPrint_Click(object sender, EventArgs e)

{

    Graphics g1 = this.CreateGraphics();

    Image MyImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, g1);

    Graphics g2 = Graphics.FromImage(MyImage);

    IntPtr dc1 = g1.GetHdc();

    IntPtr dc2 = g2.GetHdc();

    BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);

    g1.ReleaseHdc(dc1);

    g2.ReleaseHdc(dc2);

    MyImage.Save(@"c:\PrintPage.jpg", ImageFormat.Jpeg);

    FileStream fileStream = new FileStream(@"c:\PrintPage.jpg", FileMode.Open, FileAccess.Read);

    StartPrint(fileStream, "Image");

    fileStream.Close();

    if (System.IO.File.Exists(@"c:\PrintPage.jpg"))

    {

        System.IO.File.Delete(@"c:\PrintPage.jpg");

    }
}

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

public void StartPrint(Stream streamToPrint, string streamType)

{

    this.printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);

    this.streamToPrint = streamToPrint;

    this.streamType = streamType;

    System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();

    PrintDialog1.AllowSomePages = true;

    PrintDialog1.ShowHelp = true;

    PrintDialog1.Document = printDoc;

    DialogResult result = PrintDialog1.ShowDialog();

    if (result == DialogResult.OK)

    {

        printDoc.Print();

        //docToPrint.Print();

    }
}

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

2.gif

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

3.gif


Summary


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


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

This exception occure A generic error occurred in GDI+ in code line MyImage.Save(@"c:\PrintPage.jpg", ImageFormat.Jpeg); please help me rhtdmrai@yahoo.in

Posted by Amit Rai Mar 22, 2012

i downloaded the sourcecode that you have provided. when i run it then it shows no errors .but when i click on print button then it gives no response about printdialogbox.when i checked in c drive i had seen the image of printform but why i dont get printdialog.can u plz help me with it asap?

Posted by bheeshma thapa Mar 02, 2012

i downloaded the sourcecode that you have provided. when i run it then it shows no errors .but when i click on print button then it gives no response about printdialogbox.when i checked in c drive i had seen the image of printform but why i dont get printdialog.can u plz help me with it asap?

Posted by bheeshma thapa Mar 02, 2012

Ma'am I am designing Payment module for College Administration System in c#.net. I am stuck In generating Receipt on Print Button click event. Receipt should contains different particulars(Admission Fee, Tuition Fee etc.) with their respective amount. and these field are in datagrid of form. so Please Can you helped me out. dip_pathade09@yahoo.in

Posted by Asmit Pathade Feb 06, 2012

the code you provided works well,but it out of bounds of the paper. kindly help

Posted by Irfan Khan Sep 22, 2011
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Become a Sponsor