Drawing with Forms and Printers

Brian Bischof · Apress eBooks · 2002

The drawing-related classes in .NET give you the ability to draw images on forms and printed documents. Rather than adding controls and graphics to the form in design mode or using a report writer, you can write programming code to create dynamic graphics for both formats. The .NET Framework implements drawing and printing using classes derived from the System.Drawing class. This benefits you because once you learn how to draw to a form, you can use the same techniques to send output to a printer. All the graphics-related classes in this chapter are in the System.Drawing namespace. This namespace is automatically included in the VB .NET and C# Windows Form applications. It is not necessary to use this namespace qualifier when you reference the classes in a Windows Form application. Drawing and printing with Windows is made possible with the Graphical Device Interface (GDI). The GDI originated with the first version of as a way for programmers to send output to different devices without having to worry about the complexity of device compatibility issues. This made it Windows’ responsibility to manage the low-level calls to the video and printer drivers. Because GDI was developed in Windows’ infancy, it was created for C developers and thus had functional implementation similar to the C language. The .NET Framework has revamped GDI and given it an object-oriented face-lift. You now have GDI+, which is an object-oriented implementation of GDI. GDI+ uses a multitude of classes that implement graphics functionality. These classes are derived from the System.Drawing class and are shared amongst each other through the different parameter lists. The VB 6.0 graphics objects are now gone. This is a huge benefit to programmers. The graphic primitives, such as Line and Circle, are a thing of the past. You no longer have to use Win32 API calls for graphics operations. The VB 6.0 graphics objects have all been replaced by the powerful classes in the System.Drawing namespace. The core object that you work with is the Graphics object. It is the canvas that you paint your graphics onto. You use this object when drawing to a form or printing a page. Many objects are used to display graphics on the Graphics object, including Pen, Brush, and Image. There are too many of these objects to cover in this chapter, but you learn how to use the basic ones here. Learning the basic tools makes researching the other ones in MSDN much easier. To learn how to draw and print, you need to learn how to work with the Graphics object. Ideally, that would be the first topic this chapter covers. However, it is a little hard to learn how to use the object if you don’t know how to see the results of what it does. As a result, this chapter first shows how to print an object to a form. Next, it shows how to send information to the printer. After demonstrating how to output to both of these devices, the chapter goes into the details of using the Graphics object. Because the same Graphics object is used for outputting to both forms and printing, the examples in the first two sections only demonstrate printing a simple bitmap. The “Drawing Graphics” section of this chapter has examples to create more complex output. These examples can be usedwith either method. In fact, you can even make a generic class for handling graphics and it can be called for either a form or a print document.

Read the paper · More papers on PaperTik