C# Graphics - A GDI+ world
Hi all,
GDI+ is the next version of GDI. GDI+ drawing has graphics classes that can be used
to write graphics on screen.GDI+ resides in the assembly System.Drawing.dll.
The namespace
System.Drawing.dll is the assembly to add in VS.net.
Add a reference to the System.Drawing.dll using Project->Add Reference menu.
System.Drawing is the namespace that has to be added at the beginning.
using System.Drawing;
There are few more advanced classes that come under System.Drawing. Following are
the main 3 classes.
1. System.Drawing.Drawing2D
2. System.Drawing.Imaging
3. System.Drawing.Text
The surface object.
Before writing anything to the screen, we must need a surface. The graphics class
provides the surface to write on it.
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
}
After creating the graphics references, you can write graphics on it. Graphics in
the sense,
lines, arcs, circle, points etc....
Here is a list of such methods under graphics class
DrawLine - To draw line.
DrawPie - To draw pie diagram.
DrawPolygon - To draw polygon.
DrawRectangle - To draw rectangle.
DrawString - To draw string.
DrawArc - To draw arc in the ellipse.
DrawCurve - To draw a curves from array of points.
DrawEllipse - To draw ellipse.
DrawImage - To draw an image.
FillEllipse - To fill the inner part of an ellipse.
FillPie - To fill the inner part of a pie.
FillPolygon - To fill the inner part a polygon.
FillRectangle - To fill the inner part of a rectangle.
FillRegion - To fill the inner part of a Region.
After creating a Graphics object, you can use it to draw lines, draw strings, fill
rectangles and so on.
Following are some of the commonly used objects:
Brush Used to fill enclosed surfaces.
Brushes Brushes for all the standard colors.
Pen Used to draw lines and polygons, just like pen
Pens Pens for all the standard colors.
Font Font class to render text
Color Color class that has all colors objects.
Bitmap Bitmap is an object used to work with images defined by pixel.
FontFamily Defines a group of types font faces.
SolidBrush Brush of a single color
PEN class
A pen draws a line between specified points with specified width and color.
Pen pen = new Pen(Color.Black);
Pen pen = new Pen(Brushes.Blue);
Pen pen = new Pen(Color.Black, 2);
Pen pen = new Pen(Brushes.Blue, 4.7);
Brush class
Brush class is used to draw/fill inner part of any circle or rectangle area.
It has some derived classes
1. SolidBrush - brush of a single color
2. TextureBrush - used to fill the interior of a shape
3. RectangleGradientBrush
4. LinearGradientBrush.
example for brush class
Brush brsh = new SolidBrush(Color.Brown);
Brush brsh = Brushes.DarkBlue;
Rectangle class
The Rectangle class is used to draw a rectangle on the screen.
Rectangle rect = new Rectangle(2, 6, 20, 25);
Draws a rectangle starts from (2,6) with height and width of (20,25).
Point Class
Point class draws a point on the screen
Point p = new Point(5,5);
Drawing a line
DrawLine method of the Graphics class draws a line.
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics ;
Pen pen = new Pen( Color.Red );
Point point1 = new Point( 5, 10);
Point point2 = new Point( 50, 60);
g.DrawLine( pen, point1, point2 );
}
Drawing an Ellipse
DrawEllipse method is used to draw ellipse or circle.
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics ;
Pen pen = new Pen( Color.Black,3 );
Rectangle rect = new Rectangle(4, 4, 100, 100);
g.DrawEllipse( pen, rect );
}
Drawing strings
OnPaint method can be overridden to draw text/string on to the screen.
protected override void OnPaint(PaintEventArgs e)
{
Font f = new Font("Veranda", 25);
Graphics g = e.Graphics;
g.DrawString("Sample Text", f, new SolidBrush(Color.Cyan), 50,50);
}
This gives a basic ground knowledge on GDI+ graphics.
Hope this will help to explore more.
SAN