Antialiasing 2-D Graphics Programming with GDI+

173 Release the brush. br.Dispose End Sub Figure 4-13. The display drawn by the code in Ex a m ple 4 - 1 2

4.6.6 Antialiasing

Antialiasing is a technique for making the edges of graphics figures appear less jagged. To turn on antialiasing, set the Graphics objects SmoothingMode property to SmoothingMode.AntiAlias . SmoothingMode is an enumeration defined in the System.Drawing.Drawing2D namespace. Compare the arcs shown in Figur e 4 - 14 . Both arcs were drawn by calling the DrawArc method of the Graphics class, but the arc on the left was drawn with the SmoothingMode property set to SmoothingMode.None the default, and the arc on the right was drawn with the SmoothingMode property set to SmoothingMode.AntiAlias . Figur e 4- 15 shows a close-up comparison view of the upper portion of both arcs. Figure 4-14. Nonantialiased versus antialiased arcs Figure 4-15. Close-up view of nonantialiased and antialiased arcs As Figur e 4- 15 shows, antialiasing appears to improve pixel resolution by using gradient shades of the color being rendered and of the background color in this case, black and white, respectively. The downside to antialiasing is that it takes more time to render. 174

4.7 Printing

Most Visual Basic .NET programs will never need to use the .NET Frameworks native printing capabilities. Reporting tools such as Crystal Reports, as well as RAD tools for laying out reports, provide most of the printing facilities that typical Visual Basic .NET programs need. However, for the cases in which a reporting tool is not flexible enough, this section describes the .NET Frameworks support for outputting text and graphics directly to a printer.

4.7.1 Hello, Printer

Ex am ple 4- 13 shows a minimal printing example. Example 4-13. Hello, Printer Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Drawing.Printing ... These two lines initiate printing. Place this code in an appropriate place in the application. Dim pd As New HelloPrintDocument pd.Print ... This class manages the printing process. Public Class HelloPrintDocument Inherits PrintDocument Protected Overrides Sub OnPrintPageByVal e As PrintPageEventArgs MyBase.OnPrintPagee Draw text to the printer graphics device. Dim fnt As New FontArial, 10, FontStyle.Regular, _ GraphicsUnit.Point e.Graphics.DrawStringHello, Printer, fnt, Brushes.Black, 0, 0 fnt.Dispose Indicate that there are no more pages. e.HasMorePages = False End Sub End Class Printing is managed by defining a class that inherits from the PrintDocument class defined in the System.Drawing.Printing namespace. Printing is initiated by instantiating the derived class and calling its Print method inherited from the PrintDocument class. The Print method repeatedly calls the OnPrintPage method, until the HasMorePages property of the PrintPageEventArgs parameter is set to False . It is the job of the OnPrintPage method to generate each page of output that is sent to the printer. Take a closer look at the OnPrintPage method in Ex am ple 4- 13 , starting with the first line: MyBase.OnPrintPagee This line of code calls the OnPrintPage method implemented by the base PrintDocument class, passing it the same argument that was passed into the derived classs OnPrintPage method. This call is important because the PrintDocument classs OnPrintPage method is responsible for firing the