wcard
wcard

Reputation: 37

C# Print dialog is always using default printer despite user selection

I'll preface this by saying, as a beginner, getting my program to actually print was more of a pain for me than I anticipated and I am just happy to have it working.

If there is a simple solution to prevent my code from always using the default printer I would be greatful. If there isn't and I need to rework it, then I will just consider this a great learning opportunity :) Either way any assistance you can provide will be greatly appreciated.

Here is my code:

void PrintImage(object o, PrintPageEventArgs e)
{
    int x = SystemInformation.WorkingArea.X;
    int y = SystemInformation.WorkingArea.Y;
    int width = this.Width;
    int height = this.Height;
    Rectangle bounds = new Rectangle(x, y, width, height);
    Bitmap img = new Bitmap(width, height);

    this.DrawToBitmap(img, bounds);
    Point p = new Point(75, 75);
    e.Graphics.DrawImage(img, p);
}

private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
    DialogResult result = printFD.ShowDialog();
    if (result == DialogResult.OK)
    {
        PrintDocument pd = new PrintDocument();
        pd.PrintPage += new PrintPageEventHandler(PrintImage);
        pd.Print();
    }
}

I have a print dialog box and users can select their printer, but as I mentioned the default printer is always used no matter the selection. To elaborate a bit and perhaps narrow the scope of this solution, I only really want users to be able to select our .pdf printer.

Thanks in advance for any assistance you can provide. I am quite new to this community, but the wealth of knowledge here and the quick responses have already amazed me. I am truly grateful for the help you provide to beginners such as myself. Cheers!

Upvotes: 1

Views: 10442

Answers (1)

Matt T
Matt T

Reputation: 511

You need to add this to printToolStripMenuItem_Click, before pd.Print():

pd.PrinterSettings = printFD.PrinterSettings;

Hope that helps!

Upvotes: 4

Related Questions