Date: 02-16-2022
Return to Index
created by gbSnippets
GDI+ Class and Interfaces in .NET
In Microsoft .NET library, all classes (types) are grouped in namespaces. A namespace is nothing but a category of similar kind of classes. For example, Forms related classes are stored in Windows.Forms namespace, database related classed are grouped in Data and its sub namespaces such as System.Data.SqlClient, System.Data.OleDb, and System.Data.Common. Similarly, GDI+ classes are grouped under six namespaces, which reside in System.Drawing.dll assembly.
GDI+ Namespaces
GDI+ is defined in the Drawing namespace and its five sub namespaces. All drawing code resides in System.Drawing.DLL assembly. These namespaces System.Drawing, System.Drawing.Design, System.Drawing.Printing, System.Drawing.Imaging, System.Drawing.Drawing2D and System.Drawing.Text namespaces.
Now 's take a quick overview of these namespaces.
System.Drawing Namespace
The System.Drawing namespace provides basic GDI+ functionality. If contains the definition of basic classes such as Brush, Pen, Graphics, Bitmap, Font etc. The Graphics class plays a major role in GDI+ and contains methods for drawing to the display device. The following table contains some of the System.Drawing namespace classes, structures and their definition.
Classes
Classes
Description
Bitmap, Image
Bitmap and image classes.
Brush, Brushes
Brush classes used define objects to fill GDI objects such as rectangles, ellipses, pies, polygons, and paths.
Font, FontFamily
Defines a particular format for text, including font face, size, and style attributes. Not inheritable.
Graphics
Encapsulates a GDI+ drawing surface. Not inheritable.
Pen
Defines an object used to draw lines and curves. Not inheritable.
SolidBrush, TextureBrush,
Defines a brush of a single color. Brushes are used to fill graphics shapes, such as rectangles, ellipses, pies, polygons, and paths. Not inheritable.
Structures
Structure
Description
Color
Represents an ARGB color.
Point, PointF
Represents a 2D x- and y-coordinates. Point takes x, y values as a number. You can use PointF if you want to use floating number values.
Rectangle, RectangleF
Represents a rectangle with integer values. A rectangle represents two point pair - top, left and bottom, right. You can use floating values in RectangleF.
Size
Size of a rectangular region with an ordered pair of width and height. Size takes an integer as width and height while SizeF takes floating numbers to represent width and height.
System.Drawing.Design Namespace
The System.Drawing.Design namespace is somewhat smaller in compare to the System.Drawing. It xtends design-time user interface (UI) logic and drawing functionality and provides classes for customizing toolbox and editor classes. For beginners there is nothing in this namespace. At present (.NET Beta 2) it has two types of classes -
Editor Classes
BitmapEditor, FontEditor, and ImageEditor are the editor classes. You can use these classes to extend the functionality and provide an option in properties window to edit images and fonts.
ToolBox Classes
ToolBoxItem, ToolBoxItemCollection are two major toolbox classes. By using these classes you can extend the functionality of toolbox and provide the implementation of toolbox items.
System.Drawing.Drawing2D Namespace
This namespace consists classes and enumerations for advanced 2-dimmensional and vector graphics functionality. It contains classes for gradient brushes, matrix and transformation and graphics path. Some of the common classes and enumerations are defined in the following tables -
Classes
Class
Description
Blend and ColorBlend
These classes define the blend for gradient brushes. The ColorBlend defines array of colors and position for multi-color gradient.
GraphicsPath
This class represents a set of connected lines and curves.
HatchBrush
A brush with hatch style, a foreground color, and a background color.
LinearGradientBrush
Provides a brush functionality with linear gradient.
Matrix
3x3 matrix represents geometric transformation.
Enumerations
Enumeration
Description
CombineMode
Different clipping types
CompositingQuality
The quality of compositing
DashStyle
The style of dashed lines drawn with a Pen.
HatchStyle
Represents different patterns available for HatchBrush
QualityMode
Specifies the quality of GDI+ objects.
SmoothingMode
Specifies the quality of GDI+ objects.
System.Drawing.Imaging Namespace
This namespace provides advanced GDI+ imaging functionality. It defines classes for metafile images. Other classes are encoder and decoder, which let you use any image format. It also defines a class PropertyItem, which let you store and retrieve information about the image files.
System.Drawing.Printing Namespace
The System.Drawing.Printing namespace defines classes for printing functionality in your applications. Some of its major classes are defines in the following table -
Classes
Class
Description
PageSettings
Page settings
PaperSize
Size of a paper.
PreviewPageInfo
Print preview information for a single page.
PrintController
Controls document printing
PrintDocument
Sends output to a printer.
PrinterResolution
Sets resolution of a printer.
PrinterSettings
Printer settings
System.Drawing.Text Namespace
Even though most of the 's functionality is defined in System.Drawing namespace, this provides advanced typography functionality such as creating collection of fonts. Right now, this class has only three classes - FontCollection, InstalledFontCollection, and PrivateFontCollection. As you can see all of these classes are self-explanatory
The Graphics Class
The Graphics class is center of all GDI+ classes. After discussing graphics class, 'll discuss some common GDI+ objects and their representation and then we'll see some sample applications to apply this theory in our applications and how it works.
The Graphics class plays a vital role in GDI+. No matter where you go, you go through this class ;). The Graphics class encapsulates GDI+ drawing surfaces. Before drawing any object (for example circle, or rectangle ) we have to create a surface using Graphics class.
're different of ways to get a graphics object in your application. You can either get a graphics object on your form's paint event or by overriding OnPaint() method of a form. These both have one argument of type System.Windows.Forms.PaintEventArgs. You call its Graphics member to get the graphics object in your application. For example:
Collapse
protected overrides sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
Dim g As Graphics = e.Graphics
End Sub
Now 've the Graphics object. Now you can do any thing you want. The Graphics class has tons of methods to drawing graphics objects such as fonts, pens, lines, path and polygons, images and ellipse and so on. Some of the graphics class members are described in the following table -
DrawArc
This method draws an arc.
DrawBezier, DrawBeziers, DrawCurve
These methods draw a simple and bazier curves. These curvers can be closed, cubic and so on.
DrawEllipse
Draws an ellipse or circle.
DrawImage
Draws an image.
DrawLine
Draws a line.
DrawPath
Draws the path (lines with GraphicsPath )
DrawPie
Draws the outline of a pie section.
DrawPolygon
Draws the outline of a polygon.
DrawRectangle
Draws the outline of a rectangle.
DrawString
Draws a string.
FillEllipse
Fills the interior of an ellipse defined by a bounding rectangle.
FillPath
Fills the interior of a path.
FillPie
Fills the interior of a pie section.
FillPolygon
Fills the interior of a polygon defined by an array of points.
FillRectangle
Fills the interior of a rectangle with a Brush
FillRectangles
Fills the interiors of a series of rectangles with a Brush
FillRegion
Fills the interior of a Region.
Now say, if you want to draw an ellipse using the same method 've described above, you override OnPaint method and write the following code -
Collapse
protected overrides sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
Dim g As Graphics = e.Graphics
Dim pn As Pen = New Pen(Color.Green, 10)
g.DrawLine(pn, 100, 10, 30, 10)
g.DrawEllipse(New Pen(Color.Red, 20), 20, 40, 20, 20)
End Sub
That will draw a ellipse. Before we see more samples, 's discuss some GDI+ objects.
Common Graphics Objects
The graphics objects are the objects, which you use to draw your GDI+ items such as images, lines, rectangles, and path. For example, to fill a rectangle with a color, you need a color object and type of style you want to fill such as solid, texture and so on.
There are four common GDI+ objects, which 'll be using throughout your GDI+ life to fill GDI+ items. The major objects are:
Brush
Used to fill enclosed surfaces with patterns, colors, or bitmaps.
Pen
Used to draw lines and polygons, including rectangles, arcs, and pies
Font
Used to describe the font to be used to render text
Color
Used to describe the color used to render a particular object. In GDI+ color can be alpha blended
In GDI+, each of these object is represented by a class (also called type).
The Pen Class
A pen draws a line of specified width and style. You always use Pen constructor to create a pen. The constructor initializes a new instance of the Pen class. You can initialize it with a color or brush.
Initializes a new instance of the Pen class with the specified color.
Collapse
Public Sub New(Color)
Initializes a new instance of the Pen class with the specified Brush.
Collapse
Public Sub New(Brush)
Initializes a new instance of the Pen class with the specified Brush and width.
Collapse
Public Sub New(Brush, Single)
Initializes a new instance of the Pen class with the
Collapse
Dim pn as Pen = new Pen( Color.Blue )
or
Dim pn as Pen = new Pen( Color.Blue, 100 )
specified Color and Width.
Collapse
Public Sub New(Color, Single)
Here is one example:
Some of its most commonly used properties are:
Alignment
Gets or sets the alignment for objects drawn with this Pen.
Brush
Gets or sets the Brush that determines attributes of this Pen.
Color
Gets or sets the color of this Pen.
Width
Gets or sets the width of this Pen.
The Color Structure
A Color structure represents an ARGB color. Here are ARGB properties of it:
A
Gets the alpha component value for this Color.
B
Gets the blue component value for this Color.
G
'gbs_00965
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm