|
It needs to be used with the printDialog control.
You help me look at the following code:
public void StartPrint (Stream streamToPrint, string streamType)
{
this.streamToPrint = streamToPrint;
this.streamType = streamType;
// Allow the user to choose the page range he or she would
// like to print.
System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog (); // Create an instance of PrintDialog.
PrintDialog1.AllowSomePages = true;
// Show the help button.
PrintDialog1.ShowHelp = true;
// Set the Document property to the PrintDocument for
// which the PrintPage Event has been handled. To display the
// dialog, either this property or the PrinterSettings property
// must be set
PrintDialog1.Document = docToPrint; // Set the Document property of PrintDialog to the instance of PrintDocument configured above
DialogResult result = PrintDialog1.ShowDialog (); // Call the ShowDialog function of PrintDialog to display the print dialog
// If the result is OK then print the document.
if (result == DialogResult.OK)
{
docToPrint.Print (); // Start printing
}
}
// The PrintDialog will print the document
// by handling the document ’s PrintPage event.
private void docToPrint_PrintPage (object sender,
System.Drawing.Printing.PrintPageEventArgs e) // Set the event handler function for the printer to start printing
{
// Insert code to render the page here.
// This code will be called when the control is drawn.
// The following code will render a simple
// message on the printed document
switch (this.streamType)
{
case "txt":
string text = null;
System.Drawing.Font printFont = new System.Drawing.Font
("Arial", 35, System.Drawing.FontStyle.Regular);
// Draw the content.
System.IO.StreamReader streamReader = new StreamReader (this.streamToPrint);
text = streamReader.ReadToEnd ();
e.Graphics.DrawString (text, printFont, System.Drawing.Brushes.Black, e.MarginBounds.X, e.MarginBounds.Y);
break;
case "image":
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);
break;
default:
break;
}
How to trigger it in the end
I use a command button and I learn the code to "open the dialog":
With printDialog1.ShowDialog ();
No way
Please help, thanks |
|