Printer Settings in GDI+ and VB.NET

In this article I will explain about printer settings using VB and GDI+.
  • 4876
 
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 Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)

        'See if any printers are installed
        If PrinterSettngs.InstalledPrinters.Count <= 0 Then
            MessageBox.Show("Printer not found")
            Return
        End If

        'Get all the available printers and add them to the combo box
        For Each printer As [String] In PrinterSettings.InstalledPrinters
            PrintersList.Items.Add(printer.ToString())
        Next
    End Sub

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 Sub button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        'If no printer is selected
        If PrintersListed.Text = String.Empty Then
            MessageBox.Show("Select a printer from the list")
            Return
        End If

        'Get the current selected printer form the list of printers
        Dim str As String = PrintersList.SelectedItem.ToString()

        'Create a PrinterSettings objects
        Dim ps As New PrinterSettings()

        'Set the current printer
        ps.PrinterName = str

        'Read all printer resolutions and add them to the list box
        For Each pr As PrinterResolutions In ps.PrinterResolutions
            ResolutionsList.Items.Add(pr, ToString())
        Next
    End Sub

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 Sub button3_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        'If no printer is selected
        If PrinterList.Text = String.Empty Then
            MessageBox.Show("Select a printer from the list")
            Return
        End If

        'Create a printer settings
        Dim prs As New PrinterSettings()

        'Get the current selected printer from the list of printers
        Dim str As String = PrintersList.SelectedItem.ToString()
        prs.PrinterName = str

        'Read paper sizes and add them to the list box
        For Each ps As PaperSize In prs.PaperSizes
            PaperSizesList.Items.Add(ps.ToString())
        Next
    End Sub

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 Sub GetProperties_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        'If no printer is selected
        'If no printer is selected
        If PrinterList.Text = String.Empty Then
            MessageBox.Show("Select a printer from the list")
            Return
        End If

        Dim ps As New PrinterSettings()
        Dim str As String = PrintersList.SelectedItem.ToString()
        ps.PrinterName = str

        'Check if the printer is valid
        If Not ps.IsValid Then
            MessageBox.Show("Not a valid printer")
            Return
        End If

        '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 Then
            IsDefPrinterChkBox.Checked = True
        Else
            IsDefPrinterChkBox.Checked = False
        End If

        'If printer is a plotter
        If ps.IsPlotter Then
            IsPlotterChkBox.Checked = True
        Else
            IsPlotterChkBox.Checked = False
        End If

        'Duplex printing possible?
        If ps.CanDuplex Then
            CanDuplexChkBox.Checked = True
        Else
            CanDuplexChkBox.Checked = False
        End If

        'Collate?
        If ps.Collate Then
            CollateChkBox.Checked = True
        Else
            CollateChkBox.Checked = False
        End If

        'Printer valid?
        If ps.IsValid Then
            IsValidChkBox.Checked = True
        Else
            IsValidChkBox.Checked = False
        End If

        'Color printer?
        If ps.SupportsColor Then
            SuppColorsChkBox.Checked = True
        Else
            SuppColorsChkBox.Checked = False
        End If
    End Sub

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.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.