[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Select printer resolution in C#

[Select printer resolution in C#]

This example shows how to let the user select a printer resolution. When you set a PrintDocument object's printer, its DefaultPageSettings.PrinterSettings.PrinterResolutions collection contains PrinterResolution objects representing the printer's available resolutions. For example, you can select one of these to print at draft resolution and save some time and toner.

When it starts, this program uses the following code to list the available printers in the cboPrinter ComboBox.

// List the available printers. private void Form1_Load(object sender, EventArgs e) { foreach (string printer in PrinterSettings.InstalledPrinters) cboPrinter.Items.Add(printer); }

This code simply loops through the System.Drawing.Printing.PrinterSettings.InstalledPrinters collection adding the names of the printers to the ComboBox.

When the user selects a printer, the following code displays that printer's resolutions in the cboResolution ComboBox.

// Display this printer's available resolutions. private void cboPrinter_SelectedIndexChanged( object sender, EventArgs e) { // Select the printer. pdocSmiley.PrinterSettings.PrinterName = cboPrinter.Text; // Display the available resolutions. cboResolution.Items.Clear(); foreach (PrinterResolution resolution in pdocSmiley.DefaultPageSettings. PrinterSettings.PrinterResolutions) { cboResolution.Items.Add(resolution.ToString()); } // Enable the Print button if a printer and // resolution are selected. btnPrint.Enabled = ( (cboPrinter.SelectedIndex > -1) && (cboResolution.SelectedIndex > -1)); }

The code first sets the PrintDocument object's printer to the one selected by the user. It then loops through the document's printer resolutions adding them to the ComboBox.

Finally when the user clicks Print, the following code prints on the selected printer at the selected resolution.

// Print. private void btnPrint_Click(object sender, EventArgs e) { // Select the printer. pdocSmiley.PrinterSettings.PrinterName = cboPrinter.Text; // Pick the selected resolution. pdocSmiley.DefaultPageSettings.PrinterResolution = pdocSmiley.DefaultPageSettings.PrinterSettings. PrinterResolutions[cboResolution.SelectedIndex]; // Print. pdocSmiley.Print(); }

This code sets the PrintDocument object's printer name and resolution to the values selected in the corresponding ComboBoxes. It then calls the document's Print method to send the printout to the printer.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.