Drawing Graphics in Visual Basic.Net
In Visual Basic.Net you can draw lines, circles, rectangles, etc. on your screen or on a printer document by using GDI+. Drawing is done by following these two steps:
- Define a Graphics object
- Use the Graphics object to do the drawing
The Graphics object in Visual Basic is a powerful object, representing a GDI+ drawing surface. Each instance of a Graphics object is associated with a specific device context. According to Wikipedia, a device context is the following:
A Device Context (DC) is used to define the attributes of text and images that are output to the screen or printer. The actual context is maintained by GDI. A handle to the Device Context (HDC) is obtained before output is written and then released after elements have been written.
A DC, like most GDI objects, is opaque - its data cannot be accessed directly, but its handle can be passed to various GDI functions that will operate on it, either to draw an object, to retrieve information about it, or to change the object in some way.
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. See Terms of Use for details. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.
How to create a Graphics object?
A Graphics object can be created in three different ways:
- Use the Graphics reference from a PaintEventArgs parameter in the Paint event of a Form or Control. This is the usual way to obtain a Graphics object when you want to draw to a Form of Control which isn't drawn yet.
- You can also use the CreateGraphics method of a Form or control to get a Graphics object to draw on the specific form or Control. This technique is mostly used to draw on a Form or Control that already exists on the screen (in contrary to the previous method).
- Create a Graphics object inheriting from any Image object. This technique is used to draw images from scratch, or for modifying existing images. On this site you can also find an article about creating an Image from scratch.
After creating the Graphics object
When created, you can use the Graphics object to draw lines, circles, rectangles and polygones. This drawing is done by using a Pen object. Filling areas of graphics like filled shapes or text is done by using a Brush object. To define the way text looks, you also have to use the Font object. The Color object is used to define colours for the drawing.
You can also manipulate graphics by strecthing, copying and transforming images.
|