BLOG.CSHARPHELPER.COM: Select a printer resolution such as Draft or High in C#
Select a printer resolution such as Draft or High in C#
When you set a PrintDocument'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 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 is selected. btnPrint.Enabled = (cboPrinter.SelectedIndex > -1); }
The code first sets the PrintDocument'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 the Print button, the following code prints on the selected printer at the selected resolution.
This code sets the PrintDocument's printer resolution to value selected in the corresponding ComboBox. It then calls the document's Print method to send the printout to the printer.
Comments