Monday, January 31, 2011

GDI+ Introducation - C#

What is GDI+ :

This technique is the upgraded version of the GDI are used in the drawing for the making new tools, or for any purpose that needs to draw such a chart ...

Principles in the GDI +:
Create a new project Windows Forms Application, and then place a button on the form and type in the event Click:
Graphics myg = this.CreateGraphics();
Pen myp = new Pen(Brushes.Red, 2);
myg.DrawLine(myp, 0, 0, this.Width, this.Height);
myg.Dispose();
Try the code ...
The code will draw a red line of the upper-right corner of the form to the lower right corner ...

 
What happened?

  1. When we declare the Graphics object to be used, we did so, as shown and as we draw outside Paint event you must use this way ...
  2. When we declare the object Pen we have specify the red color and width 2 pixel ...
  3. We draw a line by the variable myg which we declare it as Graphics by the the sub DrawLine
This sub is used as follows:
DrawLine(FirstX, FirstY, SecX, SecY);

Examples:
  • Drawing dotted frame:
Graphics myg = this.CreateGraphics();
Pen myp = new Pen(Brushes.Red, 2);
myp.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDotDot;
myg.DrawRectangle(myp, 5, 5, 100, 100);
myg.Dispose();
  • Draw a triangle:
Graphics myg = this.CreateGraphics();
Pen myp = new Pen(Brushes.Red, 2);
System.Drawing.Drawing2D.GraphicsPath mypath = new System.Drawing.Drawing2D.GraphicsPath();
mypath.AddLine(new Point(10, 10), new Point(50, 50));
mypath.AddLine(new Point(50, 50), new Point(10, 50));
mypath.AddLine(new Point(10, 50), new Point(10, 10));
myg.DrawPath(myp, mypath);
myg.Dispose();
  • Fill a rectangle:
Graphics myg = this.CreateGraphics();
myg.FillRectangle(Brushes.Gray, new Rectangle(50, 50, 400, 400));
myg.Dispose();

This was just a simple introduction to dealing with the 2D graphics library using GDI + ... And there is still more to learn

2 comments:

  1. very good start in gdi+
    thanks

    ReplyDelete
  2. very usefull notes and we need more information on this topic please post updated data . thanks for your post.
    Dot net online tutorials


    ReplyDelete