5.1. Accessing the Graphics Object

There are several ways an application can use the code from this chapter. It can execute code using the OnPaint method or Form_Paint event, or it can use code with a button or menu click event handler. If an application executes code with Form_Paint or OnPaint, you will need to include the following line at the beginning of the method.

Graphics g = e.Graphics;

If an application executes code from a button or menu click event handler or elsewhere, you will need to create a Graphics object using CreateGraphics or another method (see Chapter 3 for details) and call the Dispose method to dispose of objects when you’re finished with them. The following code snippet gives an example:

 Graphics g = this.CreateGraphics(); ...

Get Graphics Programming with GDI+ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.