ARTICLE

A Printer Settings Example in GDI+

Posted by Dinesh Beniwal Articles | GDI+ in VB.NET July 28, 2010
In this article I will explain about A Painter Settings Example in GDI+.
 
Reader Level:

On the basis of the preceding discussion of printer settings, and of printer related classes and their members, let's 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


Figure 11.8.jpg

FIGURE 11.8: The printer settings form

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.

share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor