All classes that extend the Control class support the createGraphics method, which you can use to create a Graphics object instance. The following code fragment demonstrates how to call this method from within a Form-derived class:
Graphics g = this.createGraphics();
A second approach to explicit Graphics object creation involves using a handle to a Win32 device context (HDC). Generally, the only case in which you create a Graphics object in this fashion is when you have called a Win32 method that returns an HDC.
If you simply need graphical capabilities that are not supported natively in the Graphics object, you can use the object’s getHandle method to retrieve the handle to a Win32 device context and can pass that handle transparently to the appropriate Win32 method.
If you create a Graphics object based on a previously existing handle, the Graphics object doesn't assume ownership of the handle; after you have used the object, you are responsible for freeing the handle using the appropriate Win32 function. If you use the Graphics object’s getHandle method to retrieve the object’s underlying handle, the object retains ownership of that handle and you should not try to free it.
For more information on Win32 handles and the Graphics object, see Handle-Based Operations.