Using PrintDialog in C # allows you to conveniently print programs.
The procedure is as follows:
- Create a PrintDialog instance. As follows:
System. Windows. Forms. PrintDialog PrintDialog1 = new PrintDialog ();
- Create an instance of PrintDocument as follows:
System. Drawing. Printing. PrintDocument docToPrint =
New System. Drawing. Printing. PrintDocument ();
- Set the event processing function for the printer to start printing. The original function is as follows:
Void docToPrint_PrintPage (object sender,
System. Drawing. Printing. PrintPageEventArgs e)
- Add the event handler function to the PrintPage event of PrintDocument.
DocToPrint. PrintPage + = new PrintPageEventHandler (docToPrint_PrintPage );
- Set attributes of PrintDocument, such:
PrintDialog1.AllowSomePages = true; PrintDialog1.ShowHelp = true;
- Set the Document attribute of PrintDialog to the configured PrintDocument instance:
PrintDialog1.Document = docToPrint;
- Call the ShowDialog function of PrintDialog to display the Print dialog box:
DialogResult result = PrintDialog1.ShowDialog ();
- Start printing based on the user's choice:
If (result = DialogResult. OK)
{
DocToPrint. Print ();
}
Example:
Create a PrintService instance and call the void StartPrint (Stream streamToPrint, string streamType) function to start printing. StreamToPrint indicates the content to be printed (byte stream), streamType indicates the stream type (txt indicates normal text, and image indicates image );
Using System;
Using System. Drawing. Printing;
Using System. Windows. Forms;
Using System. IO;
Namespace EDImageSystem
{
/// <Summary>
/// Summary of PrintService.
/// </Summary>
Public class PrintService
{
Public PrintService ()
{
//
// TODO: add the constructor logic here
//
This.doc ToPrint. PrintPage + = new PrintPageEventHandler (docToPrint_PrintPage );
} // Add the event processing function to PrintPage of PrintDocument
// Declare the PrintDocument object.
Private System. Drawing. Printing. PrintDocument docToPrint =
New System. Drawing. Printing. PrintDocument (); // create an instance of PrintDocument
Private System. IO. Stream