Printig Help
Printig Help
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 the windows form in jpeg format
during runtime and printing the same on a event like Print button click.
Open a new windows form project and add a new windows form. Include simple
controls(label,textbox,button) on the form.From the toolbox,include
PrintDialog,PrintDocument components in the form.
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.
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.