Graphics in the .NET Framework with Visual Basic
The .NET Framework provides the GDI+ application programming interface (API) for manipulating graphics. GDI+ is an advanced implementation of the Windows Graphics Device Interface (GDI). With GDI+ you can create graphics, draw text, and manipulate graphical images as objects.
GDI+ is designed to offer performance, as well as ease of use. You can use GDI+ to render graphical images on Windows Forms and controls. Although you cannot use GDI+ directly on Web Forms, you can display graphical images through the Image Web Server control.
Introduction to GDI+
When you create a Windows Forms control, you can use GDI+ to access and update its image. You can also use GDI+ to create your own images, independent of your application's user interface.
To draw on an image in .NET Framework, you must use the Graphics object associated with the image.
In some cases, you can directly get the image's Graphics object. For example, when you are creating a Windows Forms control, you can override the OnPaint method to access the Graphics object for the control's image.
In other cases, such as when you are creating your own image, you also need to create a graphics object. The shared FromImage method takes an image and returns a Graphics object associated with that image.
The Graphics class has many drawing- and image-manipulation methods. Some of the commonly used methods are listed below:
Methods for drawing lines: DrawArc, DrawBezier, DrawEllipse, DrawImage, DrawLine, DrawPolygon, DrawRectangle, and DrawString.
Methods for filling shapes: FillClosedCurve, FillEllipse, FillPath, FillPolygon, and FillRectangle.
Method for clearing the drawing surface: Clear.
Method for creating a new Graphics object from an image: FromImage.
Several of the methods listed above take as arguments structures or classes defined in the System.Drawing namespace. The following table lists some of the most used GDI+ classes and structures.
Class/Structure |
Description |
---|---|
Encapsulates a GDI+ bitmap, which consists of the pixel data for a graphics image and its attributes. A Bitmap is an object used to work with images defined by pixel data. |
|
Defines brushes for all the standard colors. |
|
Represents an ARGB color. |
|
Defines a particular format for text, including font face, size, and style attributes. |
|
Defines an object used to draw lines and curves. |
|
Defines pens for all the standard colors. |
|
Represents an ordered pair of integer x- and y-coordinates that defines a point in a two-dimensional plane. |
|
Stores a set of four integers that represent the location and size of a rectangle. For more advanced region functions, use a Region object. |
|
Defines a brush of a single color. Brushes are used to fill graphics shapes, such as rectangles, ellipses, pies, polygons, and paths. |
|
Each property of the TextureBrush class is a Brush object that uses an image to fill the interior of a shape. |
Resource Management
Many of the drawing classes implement IDisposable because they encapsulate unmanaged system resources. If you create a new instance of one of these classes, you should call the class's Dispose method when you are through with the object.
Alternatively, you can create the object with the Using statement, which implicitly calls the object's Dispose method. For more information, see Object Lifetime: How Objects Are Created and Destroyed and Using Statement (Visual Basic).
Related Sections
Graphics for Visual Basic 6.0 Users
Describes changes to the graphics rendering model in Visual Basic 2005.Graphics and Drawing in Windows Forms
Roadmap for using graphics in Windows Forms applications.Graphics Overview (Windows Forms)
Provides an introduction to the graphics-related managed classes.About GDI+ Managed Code
Provides information regarding the managed GDI+ classes.Using Managed Graphics Classes
Demonstrates how to complete a variety of tasks using the GDI+ managed classes.Custom Control Painting and Rendering
Details how to provide code for painting controls.Image Web Server Control Overview
Describes the control that you can use to display images on a Web Forms page and to manage the images with code.Image Editor
Provides links to topics on how to use the image editor to create image files for use in your application.
See Also
Concepts
Object Lifetime: How Objects Are Created and Destroyed