Create a Region whose boundary is the GraphicsPath


   



  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Data;
  using System.Drawing.Imaging;

  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

    }
    static void Main() 
    {
      Application.Run(new Form1());
    }

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      
      Graphics g = e.Graphics;
      g.FillRectangle(Brushes.White, this.ClientRectangle);

      GraphicsPath gp = new GraphicsPath();

      // Create an open figure
      gp.AddLine(10, 10, 10, 50);
      gp.AddLine(10, 50, 50, 50);

      // Start a new figure
      gp.StartFigure();
      gp.AddLine(60, 10, 60, 50);
      gp.AddLine(60, 50, 100, 50);
      gp.CloseFigure();

      gp.AddEllipse(new Rectangle(110, 10, 40, 40));

      // Create a Region whose boundary is the above GraphicsPath 
      Region reg = new Region(gp);

      g.FillRegion(Brushes.Green, reg);
      reg.Dispose();
      gp.Dispose();
    }

    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }

           
          


Add size and point


   


using System;
using System.Drawing;

class Class1 {
    static void Main(string[] args) {
        Point topLeft = new Point(10,10);
        Size rectangleSize = new Size(50,50);
        Point bottomRight = topLeft + rectangleSize;
        Console.WriteLine("topLeft = " + topLeft);
        Console.WriteLine("bottomRight = " + bottomRight);
        Console.WriteLine("Size = " + rectangleSize);
    }
}

           
          


Draw Rectangle

/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds

Publisher: Apress
ISBN: 159059035X
*/
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace AllCornerRect
{
public class AllCornerRect : System.Windows.Forms.Form
{

#region Class Local Variables

RealRect MyRect;

#endregion

private System.ComponentModel.Container components = null;

public AllCornerRect() {
InitializeComponent();

this.SetStyle( ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle( ControlStyles.DoubleBuffer, true);

this.MouseDown += new MouseEventHandler(this.M_down);
this.MouseUp += new MouseEventHandler(this.M_up);
this.MouseMove += new MouseEventHandler(this.M_move);

MyRect = new RealRect();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
///

/// Required method for Designer support – do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
//
// AllCornerRect
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(320, 301);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = “AllCornerRect”;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = “AllCornerRect”;
this.Load += new System.EventHandler(this.AllCornerRect_Load);

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new AllCornerRect());
}

private void AllCornerRect_Load(object sender, System.EventArgs e)
{
}

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);

Graphics G = e.Graphics;

G.DrawRectangle(Pens.Red, MyRect.Rect);
}

#region Squeek

private void M_up(object sender, MouseEventArgs e)
{
Invalidate();
}

private void M_down(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
return;

MyRect = new RealRect(e.X, e.Y);
}

private void M_move(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
return;

MyRect.EndX = e.X;
MyRect.EndY = e.Y;
Invalidate();
}

#endregion
}

public class RealRect
{

#region Class Local Variables

private Point mStart;
private Point mEnd;
private Point mRealStart;
private Point mRealEnd;
private Size mRealSize;
private Rectangle mRect;

#endregion

public RealRect(int X, int Y)
{
mStart = Point.Empty;
mEnd = Point.Empty;
mRealStart = Point.Empty;
mRealEnd = Point.Empty;
mRealSize = Size.Empty;

mStart.X = X;
mStart.Y = Y;
mRealStart.X = X;
mRealStart.Y = Y;

mRect = Rectangle.Empty;

}

public RealRect()
{
mStart = Point.Empty;
mEnd = Point.Empty;
mRealStart = Point.Empty;
mRealEnd = Point.Empty;
mRealSize = Size.Empty;

mStart.X = 0;
mStart.Y = 0;
mRealStart.X = 0;
mRealStart.Y = 0;

mRect = Rectangle.Empty;
}

///

/// Ending X Value of rectangle
///

public int EndX
{
set{ mEnd.X = value; }
}

///

/// Ending Y Value of rectangle
///

public int EndY
{
set{ mEnd.Y = value; }
}

///

/// Get the corrected rectangle
///

public Rectangle Rect
{
get
{
MakeReal();
mRect.Location = mRealStart;
mRect.Size = mRealSize;
return mRect;
}
}

private void MakeReal()
{
//Started top left, ended bottom right
if (mEnd.X > mStart.X && mEnd.Y > mStart.Y)
{
mRealStart = mStart;
mRealEnd = mEnd;
mRealSize = new Size(mRealEnd.X-mRealStart.X, mRealEnd.Y-mRealStart.Y);
return;
}

//Started bottom right, ended top left
if (mEnd.X < mStart.X && mEnd.Y < mStart.Y) { mRealEnd = mStart; mRealStart = mEnd; mRealSize = new Size(mRealEnd.X-mRealStart.X, mRealEnd.Y-mRealStart.Y); return; } //Started top right left, ended bottom left if (mEnd.X < mStart.X && mEnd.Y > mStart.Y)
{
mRealStart.X = mEnd.X;
mRealStart.Y = mStart.Y;
mRealEnd.X = mStart.X;
mRealEnd.Y = mEnd.Y;
mRealSize = new Size(mRealEnd.X-mRealStart.X, mRealEnd.Y-mRealStart.Y);
return;
}

//Started bottom left, ended top right
if (mEnd.X > mStart.X && mEnd.Y < mStart.Y) { mRealStart.X = mStart.X; mRealStart.Y = mEnd.Y; mRealEnd.X = mEnd.X; mRealEnd.Y = mStart.Y; mRealSize = new Size(mRealEnd.X-mRealStart.X, mRealEnd.Y-mRealStart.Y); return; } } } } [/csharp]

Draw Square


   

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace GDI_Basics
{
    /// <summary>
    /// Summary description for DrawSquare.
    /// </summary>
    public class DrawSquare : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.StatusBar StatusBar1;
        internal System.Windows.Forms.StatusBarPanel pnlSquares;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public DrawSquare()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.StatusBar1 = new System.Windows.Forms.StatusBar();
            this.pnlSquares = new System.Windows.Forms.StatusBarPanel();
            ((System.ComponentModel.ISupportInitialize)(this.pnlSquares)).BeginInit();
            this.SuspendLayout();
            // 
            // StatusBar1
            // 
            this.StatusBar1.Location = new System.Drawing.Point(0, 244);
            this.StatusBar1.Name = "StatusBar1";
            this.StatusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
                                                                                          this.pnlSquares});
            this.StatusBar1.ShowPanels = true;
            this.StatusBar1.Size = new System.Drawing.Size(292, 22);
            this.StatusBar1.SizingGrip = false;
            this.StatusBar1.TabIndex = 1;
            this.StatusBar1.Text = "statusBar1";
            // 
            // pnlSquares
            // 
            this.pnlSquares.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;
            this.pnlSquares.Width = 292;
            // 
            // DrawSquare
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.StatusBar1});
            this.Name = "DrawSquare";
            this.Text = "DrawSquare";
            this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.DrawSquare_MouseDown);
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.DrawSquare_Paint);
            ((System.ComponentModel.ISupportInitialize)(this.pnlSquares)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion


        // Store the squares that are painted on the form.
        ArrayList squares = new ArrayList();

        private void DrawSquare_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                // Add a square and update the screen.
                Rectangle square = new Rectangle(e.X, e.Y, 20, 20);
                squares.Add(square);
                this.Invalidate(square);
            }
            else if (e.Button == MouseButtons.Right)
            {
                // Search  for the clicked square.
                int squareNumber = 0;
                foreach (Rectangle square in squares)
                {
                    squareNumber++;
                    if (square.Contains(e.X, e.Y))
                    {
                        MessageBox.Show("Point inside square #" +
                            squareNumber.ToString());
                    }
                }
            }

        }

        private void DrawSquare_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            Pen drawingPen = new Pen(Color.Red, 10);
            foreach (Rectangle square in squares)
            {
                e.Graphics.DrawRectangle(drawingPen, square);
            }

            pnlSquares.Text = " " + squares.Count.ToString() + " squares";

        }


        [STAThread]
        static void Main() 
        {
            Application.Run(new DrawSquare());
        }
    }
}


           
          


Simplest code to draw a Rectangle


   



  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Data;

  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

    }
    static void Main() 
    {
      Application.Run(new Form1());
    }

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
      Graphics g = e.Graphics;
      g.FillRectangle(Brushes.White, this.ClientRectangle);

      Pen p = new Pen(Color.Black);
      g.DrawRectangle(p, 3, 3, 8, 7);
      p.Dispose();


    }

    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }

           
          


Work with Rectangle type

   
 
using System;
using System.Drawing;



class Program {
    static void Main(string[] args) {
        Rectangle r1 = new Rectangle(0, 0, 100, 100);
        Point pt3 = new Point(101, 101);

        if (r1.Contains(pt3))
            Console.WriteLine("Point is within the rect!");
        else
            Console.WriteLine("Point is not within the rect!");

        // Now place point in rectangle&#039;s area.
        pt3.X = 50;
        pt3.Y = 30;

        if (r1.Contains(pt3))
            Console.WriteLine("Point is within the rect!");
        else
            Console.WriteLine("Point is not within the rect!");
    }
}

    


Draw Pyramid

/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow

Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.Drawing.Drawing2D;

namespace Pyramid
{
///

/// Summary description for Pyramid.
///

public class Pyramid1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
///

/// Required designer variable.
///

private System.ComponentModel.Container components = null;

int rot = 0;
Point center = new Point(125, 100);
public Pyramid1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

this.Text = “Pyramid by Transfomation”;
//
// TODO: Add any constructor code after InitializeComponent call
//
}

///

/// Clean up any resources being used.
///

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
///

/// Required method for Designer support – do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 8);
this.button1.Name = “button1”;
this.button1.Size = new System.Drawing.Size(48, 24);
this.button1.TabIndex = 0;
this.button1.Text = “Rotate”;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Pyramid
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1});
this.Name = “Pyramid”;
this.Text = “Pyramid”;
this.ResumeLayout(false);

}
#endregion

///

/// The main entry point for the application.
///

[STAThread]
static void Main()
{
Application.Run(new Pyramid1());
}
protected override void OnPaint (System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
g.Clear(this.BackColor);

Pyramid(g);
//PyramidPathRotate(g);
g.Dispose();
}
protected void Pyramid(Graphics g)
{
Pen p = new Pen(Color.Blue);
int ten = 10;
Rectangle rc = new Rectangle(50, 90, 150, ten); // the base rectangle
g.DrawRectangle(p, rc);
for(int i = 1;i <= 7; i++) { rc.Offset(0,-ten); rc.Inflate(-ten, 0); g.DrawRectangle(p, rc); } p.Dispose(); } private void button1_Click(object sender, System.EventArgs e) { // rot++; // rot is a class member // PyramidPathRotate(CreateGraphics()); // Refresh(); } protected void PyramidPathRotate(Graphics g) { GraphicsPath gP = new GraphicsPath(); // create an empty path Pen p = new Pen(Color.Blue); int ten = 10; Rectangle rc = new Rectangle(50, 90, 150, ten); // the base rectangle gP.AddRectangle(rc); for(int i = 1;i <= 7; i++) { rc.Offset(0,-ten); rc.Inflate(-ten,0); gP.AddRectangle(rc); } Matrix m = new Matrix(); m.RotateAt(45*rot, center, MatrixOrder.Append); gP.Transform(m); g.DrawPath(p, gP); // draw the rotated path g.FillEllipse(Brushes.Red, center.X, center.Y, 3, 3); // center point p.Dispose(); } } } [/csharp]