Printer Settings in GDI+ and C#


This article has been excerpted from book "Graphics Programming with GDI+".

On the basis of the preceding discussion of printer settings, and of printer related classes and their members, lets' write an application using these classes. In this application we will display available printers, the resolutions they support, available paper sizes, and other printer properties. This application will also allow us to set printer properties.

First we create a Windows application and add a combo box, two list boxes, three buttons, six check boxes, and two text boxes to the form. The final form looks like Figure 11.8. Then we add a reference to the System.Drawing.Printing namespace.

Next we write code. The Available Printers combo box displays all available installed printers on the machine in the ListBox control. We load all installed printers on the form's load event. As Listing 11.14 shows, we use the InsalledPrinters static property of PrinterSettings, which returns all installed printer names. We check if the installed printers count is more than 0 and add the installed printers to the combo box.

LISTING 11.14: Reading all available printers


        private void Form1_Load(object sender, System.EventArgs e)
        {

            //See if any printers are installed
            if (PrinterSettngs.InstalledPrinters.Count <= 0)
            {
                MessageBox.Show("Printer not found");
                return;
            }

            //Get all the available printers and add them to the combo box
            foreach (String printer in
            PrinterSettings.InstalledPrinters)
            {
                PrintersList.Items.Add(printer.ToString());
            }
        }


The Get Printer Resolution button returns resolutions supported by a printer selected in ListBox1. The PrinterResolutions property of PrinterSettings returns the printer resolutions supported by the printer Listing 11.15 reads all available resolutions for the selected printer in ListBox1 and adds them to ListBox2.

LISTING 11.15: Reading printer resolutions


        private void button2_Click(object sender, System.EventArgs e)
        {
            //If no printer is selected
            if (PrintersListed.Text == string.Empty)
            {
                MessageBox.Show("Select a printer from the list");
                return;
            }

            //Get the current selected printer form the list of printers
            string str = PrintersList.SelectedItem.ToString();

            //Create a PrinterSettings objects
            PrinterSettings ps = new PrinterSettings();

            //Set the current printer
            ps.PrinterName = str;

            //Read all printer resolutions and add them to the list box
            foreach (PrinterResolutions pr
            in ps.PrinterResolutions)
            {
                ResolutionsList.Items.Add(pr, ToString());
            }
        }


The Get Paper Size button returns the available paper sizes. Again we use the PaperSizes property of PrinterSettings, which returns all available paper sizes. Listing 11.6 reads all available paper sizes and adds them to the list box.

LISTING 11.16: Reading paper sizes


        private void button3_Click(object sender, System.EventArgs e)
        {
            //If no printer is selected
            if (PrinterList.Text == string.Empty)
            {
                MessageBox.Show("Select a printer from the list");
                return;
            }

            //Create a printer settings
            PrinterSettings prs = new PrinterSettings();

            //Get the current selected printer from the list of printers
            string str = PrintersList.SelectedItem.ToString();
            prs.PrinterName = str;

            //Read paper sizes and add them to the list box
            foreach (PaperSize ps in prs.PaperSizes)
            {
                PaperSizesList.Items.Add(ps.ToString());
            }
        }


The Get Printer Properties buttons gets the printer properties and sets the check boxes and text box controls according to the values returned. The Get Printer Properties button click event handler code is given in Listing 11.17. We read many printer properties that were discussed earlier in this article.

LISTING 11.17: Reading printer properties


        private void GetProperties_Click(object sender, System.EventArgs e)
        {
            //If no printer is selected
            //If no printer is selected
            if (PrinterList.Text == string.Empty)
            {
                MessageBox.Show("Select a printer from the list");
                return;
            }

            PrinterSettings ps = new PrinterSettings();
            string str = PrintersList.SelectedItem.ToString();
            ps.PrinterName = str;

            //Check if the printer is valid
            if (!ps.IsValid)
            {
                MessageBox.Show("Not a valid printer");
                return;
            }

            //Set printer name and copies
            textBox1.Text = ps.PrinterName.ToString();
            textBox2.Text = ps.Copies.ToString();

            //If printer is the default printer
            if (ps.IsDefaultPrinter == true)
                IsDefPrinterChkBox.Checked = true;
            else
                IsDefPrinterChkBox.Checked = false;

            //If printer is a plotter
            if (ps.IsPlotter)
                IsPlotterChkBox.Checked = true;
            else
                IsPlotterChkBox.Checked = false;

            //Duplex printing possible?
            if (ps.CanDuplex)
                CanDuplexChkBox.Checked = true;
            else
                CanDuplexChkBox.Checked = false;

            //Collate?
            if (ps.Collate)
                CollateChkBox.Checked = true;
            else
                CollateChkBox.Checked = false;

            //Printer valid?
            if (ps.IsValid)
                IsValidChkBox.Checked = true;
            else
                IsValidChkBox.Checked = false;

            //Color printer?
            if (ps.SupportsColor)
                SuppColorsChkBox.Checked = true;
            else
                SuppColorsChkBox.Checked = false;
        }


Now let's run the application. By default, the Available Printers combo box displays all available printers. Select a printer from the list, and click the Get Printer Resolution button, which display the printer resolutions supported by the selected printer. Also click on the Get Paper Size and Get Printer Properties buttons. The final output of the application is shown in Figure 11.9.

Conclusion

Hope the article would have helped you in understanding A Painter Settings Example in GDI+. Read other articles on GDI+ on the website.

book.gif




Recommended Free Ebook
Similar Articles