Introduction of GDI(Graphics Design Interface)
GDI (Graphics Design Interface) through which we can design rectangle, circle, and more shapes with the help of programming of classes and methods. Like other languages C# also provides rich set of classes, methods and events for developing
applications with graphical capabilities.
GDI+ -
- GDI+ is the advance evolution of GDI. Microsoft has taken care of most of the GDI problems and have made it easy to use with GDI+.
- GDI+ resides in System.Drawing.dll assembly.
- All GDI+ classes are reside in the System.Drawing, System.Text, System.Printing, System.Internal , System.Imaging, System.Drawing2D and System.Design namespaces.
Graphics Classes-
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. Generally we use Paint event of a Form to get the
reference of the graphics. Another way is to override OnPaint method.
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
} |
Some Graphics Class's Methods -
DrawArc ( ) | Draws an arc from the specified ellipse. |
DrawBezier( ) | Draws a cubic bezier curve. |
DrawBeziers ( ) | Draws a series of cubic Bezier curves. |
DrawClosedCurve( ) | Draws a closed curve defined by an array of points. |
DrawCurve ( ) | Draws a curve defined by an array of points. |
DrawEllipse( ) | Draws an ellipse. |
DrawImage ( ) | Draws an image. |
DrawLine ( ) | Draws a line. |
DrawPath( ) | Draws the lines and curves defined by a 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. |
For making GDI+ application you have to first add Reference of System.Drawing.Dll like shown in below picture.
After this you have to add two namespaces -
using System.Drawing;
using System.Drawing.Drawing2D; |
Graphics Objects-
With the help of Graphics object, you can
draw lines, fill shapes, draw text and more. The major objects are given below-
Pen | Used to draw lines and polygons, including rectangles, arcs, and pies. |
Brush | Used to fill enclosed surfaces with patterns,colors, or bitmaps. |
Color | Used to describe the color used to render a particular object. In GDI+ color can be alpha blended. |
Font | Used to describe the font to be used to render text. |
Pen Class- A pen draws a line of specified width and style. You can initialize Pen with a color or brush.
Initializes a new instance of the Pen class with the specified color.
public Pen(Color);
Initializes a new instance of the Pen class with the specified Brush.
public Pen(Brush);
Initializes a new instance of the Pen class with the specified Brush and width.
public Pen(Brush, float);
Initializes a new instance of the Pen class with the specified Color and Width.
public Pen(Color, float);
Example-
Pen pn = new Pen( Color.Blue ); or Pen pn = new Pen( Color.Blue, 150 );
Properties of Pen -
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. |
Color Class-
A Color represents an ARGB color. Properties of color is given below-
A | Gets the alpha component value for this Color. |
B | Gets the blue component value for this Color. |
G | Gets the green component value for this Color. |
R | Gets the red component value for this Color. |
Example-
Pen pn = new Pen( Color.Blue );
Font Class-
The Font class defines a particular format for text such as font type, size, and style attributes.
//Initializes a new instance of the Font class with the specified attributes.
public Font(string, float);
//Initializes a new instance of the Font class from the specified existing Font and FontStyle.
public Font(Font, FontStyle); |
Example-
Font font = new Font("Times New Roman", 26);Brush Class-
The Brush class is an abstract base class and cannot be instantiated. We always use its derived classes to instantiate a brush object, like
- SolidBrush
- TextureBrush
- RectangleGradientBrush
- LinearGradientBrush.
Example-
LinearGradientBrush lBrush = new LinearGradientBrush(rect, Color.Red, Color.Yellow,
LinearGradientMode.BackwardDiagonal);
Example of Drawing a Rectangle-
protected override void OnPaint(PaintEventArgs pe)
{
Graphics gcs = pe.Graphics;
Rectangle rect = new Rectangle(30, 30, 230, 200);
LinearGradientBrush lBrush = new LinearGradientBrush(rect, Color.Bisque, Color.DarkMagenta,
LinearGradientMode.BackwardDiagonal);
gcs.FillRectangle(lBrush, rect);
}
|