1

Here is the code I use (just the printing-related part):

Button 1 onclick handler method:

printDialog1 = new PrintDialog();
printDialog1.AllowPrintToFile = true;
printDialog1.PrintToFile = false;
if (printDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    PrintDocument pd = new PrintDocument();
    pd.DefaultPageSettings.PaperSize = new PaperSize("A4", 826, 1169);
    pd.PrinterSettings.PrintToFile = true;
    pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
    pd.Print();
}

And my pd_PrintPage method:

Bitmap bitmapCanvas = new Bitmap(1000, 1000);
Graphics g = Graphics.FromImage(bitmapCanvas);
g.Clear(Color.White);
...
some g.Draw...() stuff

...
e.Graphics.DrawImage(bitmapCanvas, A(2), A(2));
//where e is the PrintPageEventArgs defined in the method signature

First part of my problem is that, this doesn't print to the selected printer (selected on the print dialog). It only prints to a printer if that is the default printer. Under Windows 7 it works, it recognizes the default printer, so the default printer will be selected by default in the comboBox on the print dialog which comes up after I click the button.

My main problem is that, this doesn't work under Windows Xp at all (unfortunately I only can use that). And I'm kind of curious why. So I don't know if I made a mess, or it is not supported under Windows Xp.

With what should I complete or correct my code?

Any help is appreciated and thank you very much! Mitulat bati

Mitulát báti
  • 2,086
  • 5
  • 23
  • 37
  • 1
    What were you expecting? Use the PrinterSettings property. – It'sNotALie. Jun 04 '13 at 20:28
  • What do you think I was expecting? What properties should I set to reach my aim? – Mitulát báti Jun 04 '13 at 20:43
  • What I told you to use, `PrintDocument.PrinterSettings`. – It'sNotALie. Jun 04 '13 at 20:45
  • You create the PrintDialog but then never use it, when you are editing your paper size you would want to operate on the printDialog1.Document Property you just create a new one which is likely to use your default printer. – Bearcat9425 Jun 04 '13 at 20:52
  • newStackExchangeInstance: PrinterSettings is a class which has properties. My question was about those properties... but I think now I see it. Thank you! – Mitulát báti Jun 04 '13 at 20:57
  • Bearcat9425: you mean, that I should use the values of the PrintDialog in the "if" section? For example: printDocument.PrinterSettings = printDialog1.PrinterSettings; ? – Mitulát báti Jun 04 '13 at 20:59
  • What I mean is you create the dialog and go through all that trouble and use nothing from it. You could do that or do what I posted below as the answer. – Bearcat9425 Jun 04 '13 at 20:59

1 Answers1

0

Try this,

printDialog1 = new PrintDialog();
printDialog1.AllowPrintToFile = true;
printDialog1.PrintToFile = false;
if (printDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    printDialog1.Document.DefaultPageSettings.PaperSize = new PaperSize("A4", 826, 1169);
    printDialog1.Document.PrinterSettings.PrintToFile = true;
    printDialog1.Document.PrintPage += new PrintPageEventHandler(pd_PrintPage);
    printDialog1.Document.Print();
}
Bearcat9425
  • 1,580
  • 1
  • 11
  • 12
  • But, I think in the meantime I found a solution. That both of you said: printDocument.PrinterSettings = printDialog1.PrinterSettings; Anyway... I will figure whether it works or not out tomorrow. – Mitulát báti Jun 04 '13 at 22:06
  • The difference is that you created a new PrintDocument in your if statement that just used the default settings, mine uses the document from your print dialog where you chose the new print and apply your settings that are needed. – Bearcat9425 Jun 05 '13 at 14:17