alonix

i'm having serious trouble doing a simple task of printing a form, as i understood this should be enough:

if (MyPrintDialog.ShowDialog() == DialogResult.OK)

{

this.MyPrintDocument.Print();

}

but... nothing! i get blank pages from some reason.

anyone seen this phenomenon before



Re: Visual C# General printing a form

Figo Fei - MSFT

Hi,

Could you pls give some detail info about MyPrintDialog and MyPrintDocument

Otherwise, here is the sample code about how to use PrintDialog and PrintDocument:

Create a PrintDialog instance:
System.Windows.Forms.PrintDialog PrintDialog1=new PrintDialog ();
Create a PrintDocument instance:
System.Drawing.Printing.PrintDocument docToPrint =
new System.Drawing.Printing.PrintDocument();
Printer's print method, the method signtrue as follows:
void docToPrint_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)

Put the method into event PrintPage of the PrintDocument
docToPrint.PrintPage+=new PrintPageEventHandler(docToPrint_PrintPage);
Set some related PrintDocument properties:
PrintDialog1.AllowSomePages = true;PrintDialog1.ShowHelp = true; 
Set the PrintDialog's Document to the instance of PrintDocument mentioned above:
PrintDialog1.Document = docToPrint;
Call PrintDialog's ShowDialog() to show the print dialog:
DialogResult result = PrintDialog1.ShowDialog(); 
If user choose ok, then print: 
if (result==DialogResult.OK)
{
docToPrint.Print();
}


For example :

First create a PrintService instance,then call void StartPrint(Stream streamToPrint,string streamType)to print. StreamToPrint is what we want to print, streamType is the type of the stream



using System;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.IO;

namespace EDImageSystem
{
public class PrintService
{
public PrintService()
{

this.docToPrint.PrintPage+=new PrintPageEventHandler(docToPrint_PrintPage);
}//Put the print method into event PrintPage of the PrintDocument 

// Declare the PrintDocument object.
private System.Drawing.Printing.PrintDocument docToPrint =
new System.Drawing.Printing.PrintDocument();//create a PrintDocument instance 

private System.IO.Stream streamToPrint;
string streamType;

// This method will set properties on the PrintDialog object and
// then display the dialog.
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 a PrintDialog instance 
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 PrintDialog's Document to the instance of PrintDocument mentioned above

DialogResult result = PrintDialog1.ShowDialog();//call PrintDialog's ShowDialog() to show the print dialog

// If the result is OK then print the document.
if (result==DialogResult.OK)
{
docToPrint.Print();//begin to print 
}

}

// The PrintDialog will print the document
// by handling the documentí»s PrintPage event.
private void docToPrint_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)//print method 
{

// 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;
}

}

}
}

 

Thank you






Re: Visual C# General printing a form

Figo Fei - MSFT

And for short you can take a look at: http://samples.gotdotnet.com/quickstart/winforms/doc/WinFormsPrinting.aspx

Hope it helps :-)






Re: Visual C# General printing a form

TheTrueFace